Observing unusual performance data when creating empty arrays using Array(length)
in Chrome (85), Firefox(79), and Safari(11.1) on a Mac OS X (10.13.4) system.
Below is the test script used;
console.time('Array(length)');
var len_int = 1;
var test_arr = Array(len_int);
console.timeEnd('Array(length)');
console.log('test_arr.length =', test_arr.length);
Testing various values of len_int
from 1 to 1000000000 [one billion]. Here are the results:
CHROME
Performance stats:
Array(length) - 1: 0.018310546875 ms
Array(length) - 10: 0.014892578125 ms
Array(length) - 100: 0.01171875 ms
Array(length) - 1000: 0.0068359375 ms
Array(length) - 10000: 0.011962890625 ms
Array(length) - 100000: 0.187744140625 ms
Array(length) - 1000000: 1.826904296875 ms
Array(length) - 10000000: 18.211181640625 ms
Array(length) - 100000000: 0.026123046875 ms
Array(length) - 1000000000: 0.0126953125 ms
FIREFOX
Performance stats:
Array(length) - 1: 1ms - timer ended
Array(length) - 10: 1ms - timer ended
Array(length) - 100: 0ms - timer ended
Array(length) - 1000: 0ms - timer ended
Array(length) - 10000: 2ms - timer ended
Array(length) - 100000: 1ms - timer ended
Array(length) - 1000000: 0ms - timer ended
Array(length) - 10000000: 0ms - timer ended
Array(length) - 100000000: 1ms - timer ended
Array(length) - 1000000000: 1ms - timer ended
SAFARI
Performance stats:
Array(length) - 1: 0.006ms
Array(length) - 10: 0.028ms
Array(length) - 100: 0.011ms
Array(length) - 1000: 0.009ms
Array(length) - 10000: 0.031ms
Array(length) - 100000: 0.252ms
Array(length) - 1000000: 1.663ms
Array(length) - 10000000: 19.373ms
Array(length) - 100000000: 696.767ms
Array(length) - 1000000000: 0.008ms
The abnormality lies in Chrome's sudden drop in performance for array sizes between 1000000 [1.8ms] and 10000000 [18ms], before returning below sub-millisecond levels for larger arrays. Safari shows a decline in performance from 1000000 [19ms] to 100000000 [696ms], but then improves again at 1000000000 [0.008ms]. Firefox, on the other hand, remains relatively consistent in performance.
Why does this anomaly occur? Is there a solution or alternate method that maintains high performance?
It's worth noting that this benchmark was run multiple times, each showing the same outcome. The same discrepancy was observed on an iPhone 7 running Safari (via BrowserStack).
This inquiry arises as I use Array(length)
to optimize scripts relying heavily on arrays, but notice a loss in performance benefits within the array size range of 1000000 to 10000000 on Chrome and Safari.