Automattic / Automattic/node-canvas
500ms lag creating first canvas on v2.3.1 (macOS)
- Dominant language
- JavaScript
- Stars
- 10.7k
- Forks
- 1.2k
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 1
Description
Hey guys, thanks for releasing node-canvas v2! Congrats on that.
On macOS (both 10.12 Sierra and 10.14 Mojave) with node-canvas v2.3.1, there is a strange 300 - 500 millisecond lag when creating the first canvas, no matter the resolution. Observe:
**macOS 10.12 Sierra**
```
#1 elapsed: 505.803266ms
#2 elapsed: 0.055942ms
Tests performed on Node.js v10.14.1, node-canvas v2.3.1 (Darwin darwin x64 16.7.0, 16384 MB RAM)
```
**macOS 10.14 Mojave**
```
#1 elapsed: 335.800062ms
#2 elapsed: 0.062919ms
Tests performed on Node.js v10.14.1, node-canvas v2.3.1 (Darwin darwin x64 18.2.0, 16384 MB RAM)
```
## Steps to Reproduce
Here is the code to produce the above output:
```js
// node-canvas v2.x timing test
const { createCanvas } = require('canvas');
var hrTimeToMilliseconds = function(start) {
var diff = process.hrtime( start );
var nano = diff[0] * 1e9 + diff[1];
return nano / (1000000000 / 1000);
};
for (var idx = 1; idx <= 2; idx++) {
var start = process.hrtime();
createCanvas( 1, 1 ).getContext('2d');
console.log( "#" + idx + " elapsed: " + hrTimeToMilliseconds(start) + "ms" );
}
const os = require('os');
console.log(
"Tests performed on Node.js " + process.version +
", node-canvas v" + require('canvas/package').version +
" (" + os.type() + " " + os.platform() + " " + os.arch() + " " +
os.release() + ", " + Math.floor( os.totalmem() / 1024 / 1024 ) + " MB RAM)"
);
```
As you can see I'm simply creating a 1x1 pixel canvas and grabbing a 2D context. I am using the Node.js high-resolution timer system to measure the createCanvas operation. The first canvas created is quite slow, where subsequent canvases in the same process are extremely fast.
This bug is not reproducible using the node-canvas 1.x branch, as you can see here:
```
#1 elapsed: 3.187736ms
#2 elapsed: 0.046084ms
Tests performed on Node.js v10.9.0, node-canvas v1.6.10 (Linux linux x64 4.14.62-65.117.amzn1.x86_64, 1957 MB RAM)
```
Nor is it reproducible in Linux, regardless of node-canvas version. This appears to be a macOS + node-canvas v2.x performance issue.
## Your Environment
Shown above as part of test script output.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.