var sTest = Array.apply(null, Array(100)).map(function (x, i) { return i; });
var mTest = Array.apply(null, Array(1000)).map(function (x, i) { return i; });
var lTest = Array.apply(null, Array(10000)).map(function (x, i) { return i; });
let result = [ sTest, 0 ];
let result = [ mTest, 0 ];
let result = [ lTest, 0 ];
sTest.push(0);
mTest.push(0);
lTest.push(0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Small test: array deconstruct | |
Medium test: array deconstruct | |
Large test: array deconstruct | |
Small test: array push | |
Medium test: array push | |
Large test: array push |
Test name | Executions per second |
---|---|
Small test: array deconstruct | 5833384.0 Ops/sec |
Medium test: array deconstruct | 1050047.1 Ops/sec |
Large test: array deconstruct | 145508.8 Ops/sec |
Small test: array push | 14408392.0 Ops/sec |
Medium test: array push | 14007903.0 Ops/sec |
Large test: array push | 13754903.0 Ops/sec |
Overview
The provided benchmark measures the performance of two approaches in JavaScript: array deconstruction and array push. The goal is to determine which approach is more efficient, especially when dealing with large arrays.
Test Cases
There are four test cases:
sTest
) using Array.apply(null, Array(100)).map(function (x, i) { return i; })
[...sTest, 0]
mTest
) using Array.apply(null, Array(1000)).map(function (x, i) { return i; })
[...mTest, 0]
lTest
) using Array.apply(null, Array(10000)).map(function (x, i) { return i; })
[...lTest, 0]
sTest
) using Array.apply(null, Array(100)).map(function (x, i) { return i; })
sTest.push(0)
mTest
) using Array.apply(null, Array(1000)).map(function (x, i) { return i; })
mTest.push(0)
lTest
) using Array.apply(null, Array(10000)).map(function (x, i) { return i; })
lTest.push(0)
Library and Special Features
The benchmark uses no external libraries. There are no special JavaScript features or syntax used in this benchmark.
Approach Comparison
There are two main approaches being tested:
Performance Comparison
The benchmark results show that the array push approach outperforms the array deconstruction approach, especially on large arrays. This suggests that pushing new elements to an existing array is more efficient than creating a new array by deconstructing the original array.
Alternatives
Other alternatives for improving performance when dealing with large arrays include:
push()
or unshift()
, consider resizing the array using splice()
and setting its length to a smaller value.TypedArray.prototype.set()
, for large array operations.Keep in mind that the choice of approach depends on the specific use case and performance requirements.