window.top.tests = {control:[], concat:[], spread:[]};
window.test = (new Array(100000)).fill(null);
window.cutoff = 99000;
for (let element of window.test) window.top.tests.control.push(element);
if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
window.top.tests.concat = window.top.tests.concat.concat(window.test);
if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
window.top.tests.spread.push(window.test);
if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Control (for push) | |
Concat | |
Spread |
Test name | Executions per second |
---|---|
Control (for push) | 33.3 Ops/sec |
Concat | 5606.3 Ops/sec |
Spread | 919.8 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition:
The benchmark compares three approaches to append elements to an array of size 100,000:
push()
method (Control)concat
array method (Concat)...
) to add elements to an existing array (Spread)Script Preparation Code:
window.top.tests = {control:[], concat:[], spread:[]};
window.test = (new Array(100000)).fill(null);
window.cutoff = 99000;
This code sets up three arrays on the window.top
object: control
, concat
, and spread
. It also creates an array of size 100,000 filled with null
values and assigns it to window.test
.
Html Preparation Code: (empty string)
Since there is no HTML preparation code provided, we can assume that the benchmark only tests JavaScript performance.
Individual Test Cases:
Each test case measures the execution time for a specific approach:
for (let element of window.test) window.top.tests.control.push(element);
if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
This test case uses the push()
method to append elements to the control
array. It loops through the entire window.test
array and pushes each element onto the control
array. When the length of the control
array exceeds a certain threshold (cutoff
), it resets the array.
window.top.tests.concat = window.top.tests.concat.concat(window.test);
if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
This test case uses the concat()
method to append the entire window.test
array to the concat
array. It does this by assigning a new array created from the concatenation of the existing concat
array and the window.test
array back to the concat
property.
window.top.tests.spread.push(...window.test);
if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
This test case uses the spread operator (...
) to add all elements from the window.test
array to the spread
array. The resulting array is then pushed onto the spread
array using the push()
method.
Pros and Cons of each approach:
Library/Functionality Used:
None of these test cases explicitly use any libraries beyond what is built-in to JavaScript. However, some modern browsers like Chrome may optimize certain methods for better performance (e.g., the concat()
method).
Special JS Feature/Syntax:
The spread operator (...
) is a feature introduced in ECMAScript 2015. It allows adding elements from one array to another without creating an intermediate array.
Other Alternatives:
If you wanted to test other approaches, some alternatives could include:
splice()
method instead of push()
Array.prototype.slice()
method to create a new array before appendingHowever, these alternatives would require modifying the benchmark code and may not provide meaningful results compared to the original test cases.