Open enzoferraribf opened 11 months ago
Testing a little more, I was able to verify that that behavior only happens if the array is filled somehow. I was able to verify that applying Object.seal
in an object with a LOT of properties causes the same weird performance issue.
Changing the function to be something like:
function getOptimizedArrayOfSize(size, defaultValue) {
const array = new Array(size);
// array.fill(defaultValue);
Object.seal(array);
return array;
}
Yields same results as Node
More updates, looks like the behavior of Object.seal
is different from Bun to Node.
function getOptimizedArrayOfSize(size, defaultValue) {
const array = new Array(size);
array.fill(defaultValue);
Object.seal(array);
// works in Node, outputs 'offending write'
// throws TypeError exception in Bun
array[2] = 'offending write'
console.log(array[2])
return array;
}
While I think Bun's approach is better, should that deviate from Node's?
_Unlike Object.freeze(), objects sealed with Object.seal() may have their existing properties changed, as long as they are writable._ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
Would be interesting to test if the behavior is the same in safari.
Unfortunately I can't test that in Safari, as I only have a windows machine
I was able to test in Safari. Here is the result:
[Debug] test-nonoptimized: 78.632ms [Debug] test-optimized: 1901.186ms
It seems to be something related to Webkit then.
Tested in Safari version: Version 17.1 (19616.2.9.11.7)
I was testing a little program to exemplify different allocation strategies in JS (Node).
Functions have the following code:
Finally, I'm (naively) testing both strategies with:
Running with node, I get consistent results in my machine:
test-nonoptimized: 14.158ms
test-optimized: 3.928ms
However, when running with bun, I get very weird results:
[11.67ms] test-nonoptimized
[423.54ms] test-optimized
When I remove
Object.seal(array)
from the optimized function, I get better results with bun:[11.11ms] test-nonoptimized
[10.27ms] test-optimized
Node stays the same indepedent of
Object.seal
being presentBun version: 1.0.4