Skip to content

Instantly share code, notes, and snippets.

@n8allan
Created May 12, 2025 19:59
Show Gist options
  • Save n8allan/5b51ffb4f5bfb59b261815937638b918 to your computer and use it in GitHub Desktop.
Save n8allan/5b51ffb4f5bfb59b261815937638b918 to your computer and use it in GitHub Desktop.
Javascript benchmark of using push vs. [length] for sequential insertion
// Array Performance Benchmark: push() vs array[length] = value
// This script compares the performance of two methods of adding elements to an array
function runBenchmark(iterations, arraySize) {
// Benchmark for Array.push()
const pushResults = [];
for (let test = 0; test < iterations; test++) {
const arr = [];
const startTime = performance.now();
for (let i = 0; i < arraySize; i++) {
arr.push(i);
}
const endTime = performance.now();
pushResults.push(endTime - startTime);
}
// Benchmark for arr[arr.length] = value
const indexResults = [];
for (let test = 0; test < iterations; test++) {
const arr = [];
const startTime = performance.now();
for (let i = 0; i < arraySize; i++) {
arr[arr.length] = i;
}
const endTime = performance.now();
indexResults.push(endTime - startTime);
}
return {
push: {
avg: pushResults.reduce((sum, time) => sum + time, 0) / iterations,
min: Math.min(...pushResults),
max: Math.max(...pushResults)
},
index: {
avg: indexResults.reduce((sum, time) => sum + time, 0) / iterations,
min: Math.min(...indexResults),
max: Math.max(...indexResults)
}
};
}
// Test parameters
const testSizes = [100, 1000, 10000, 100000, 1000000];
const iterations = 50; // Number of times to run each test for averaging
console.log("Array Performance Benchmark: push() vs array[length] = value");
console.log("===========================================================");
testSizes.forEach(size => {
console.log(`\nTest with array size: ${size.toLocaleString()}`);
console.log("----------------------------");
const results = runBenchmark(iterations, size);
console.log(`push(): avg: ${results.push.avg.toFixed(4)}ms, min: ${results.push.min.toFixed(4)}ms, max: ${results.push.max.toFixed(4)}ms`);
console.log(`array[length] = val: avg: ${results.index.avg.toFixed(4)}ms, min: ${results.index.min.toFixed(4)}ms, max: ${results.index.max.toFixed(4)}ms`);
const difference = ((results.index.avg / results.push.avg) - 1) * 100;
if (results.push.avg < results.index.avg) {
console.log(`push() is ${Math.abs(difference).toFixed(2)}% faster`);
} else if (results.push.avg > results.index.avg) {
console.log(`array[length] = value is ${Math.abs(difference).toFixed(2)}% faster`);
} else {
console.log("Both methods performed equally");
}
});
// Display summary
console.log("\nSummary:");
console.log("========");
console.log("This benchmark compares two methods of adding elements to an array:");
console.log("1. arr.push(value)");
console.log("2. arr[arr.length] = value");
console.log("\nEach test was run multiple times and averaged to reduce variance.");

Chrome 136.0.7103.93

TL;DR: [length] is faster until very large arrays

Array Performance Benchmark: push() vs array[length] = value

Test with array size: 100

push(): avg: 0.0060ms, min: 0.0000ms, max: 0.1000ms array[length] = val: avg: 0.0020ms, min: 0.0000ms, max: 0.1000ms array[length] = value is 66.67% faster

Test with array size: 1,000

push(): avg: 0.0320ms, min: 0.0000ms, max: 0.7000ms array[length] = val: avg: 0.0080ms, min: 0.0000ms, max: 0.1000ms array[length] = value is 75.00% faster

Test with array size: 10,000

push(): avg: 0.1000ms, min: 0.0000ms, max: 1.2000ms array[length] = val: avg: 0.0840ms, min: 0.0000ms, max: 0.4000ms array[length] = value is 16.00% faster

Test with array size: 100,000

push(): avg: 1.3060ms, min: 0.7000ms, max: 7.0000ms array[length] = val: avg: 1.1640ms, min: 0.9000ms, max: 2.5000ms array[length] = value is 10.87% faster

Test with array size: 1,000,000

push(): avg: 12.9220ms, min: 11.0000ms, max: 34.7000ms array[length] = val: avg: 15.1440ms, min: 11.7000ms, max: 23.2000ms push() is 17.20% faster

Summary:

This benchmark compares two methods of adding elements to an array:

  1. arr.push(value)
  2. arr[arr.length] = value

Each test was run multiple times and averaged to reduce variance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment