var testArray = new Array(100).fill(null).map((_, i) => `index-${i}`);
var testNewItem = `new-item`;
testArray = [testArray, testNewItem];
testArray.push(testNewItem);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
push |
Test name | Executions per second |
---|---|
spread | 876.9 Ops/sec |
push | 6425974.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is tested?
The provided benchmark tests two approaches to add an item to a large array: using the spread operator ("..."
) and using the push
method. The test measures the performance difference between these two methods on a large array of 100 elements.
Options compared
There are two options being compared:
"..."
) to create a new array with the original array's elements concatenated with the new item.push
method to add the new item to the end of the existing array.Pros and Cons
Library usage
There is no library used in this benchmark. It appears to be a simple JavaScript microbenchmark designed to compare two specific approaches.
Special JS feature or syntax
None are mentioned in the provided benchmark definition. However, it's worth noting that some browsers may exhibit different behavior when using the spread operator due to optimizations or caching.
Benchmark preparation code
The script preparation code creates a large array testArray
with 100 elements and a string variable testNewItem
. The HTML preparation code is empty, suggesting that this benchmark focuses solely on JavaScript performance.
Other alternatives
Other approaches to add an item to an array could include:
concat()
method (not included in this benchmark).Array.from()
or Array.prototype.slice()
, which might be slower due to their overhead.Keep in mind that the best approach depends on your specific use case and performance requirements. This benchmark is designed to provide a simple comparison between two widely used methods.