Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Array Mutation Test
(version: 0)
Comparing performance of:
Mutate vs Copy & Shift
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const n = 10000 var arr = new Array(n).fill(0); for (let i = 0; i < n; i++) { arr[i] = i; }
Tests:
Mutate
for (let i = 0; i < arr.length; i++) { arr[i] = i * 2; }
Copy & Shift
const copy = []; for (let i = 0; i < arr.length; i++) { const item = arr.shift(); copy.push(item * 2); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Mutate
Copy & Shift
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Mutate
861.2 Ops/sec
Copy & Shift
14772017.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is designed to test how efficiently JavaScript arrays can be mutated. The script preparation code initializes an array of 10,000 zeros and fills it with incremental values from 0 to 9,999 using a for loop. **Script Preparation Code** ```javascript const n = 10000; var arr = new Array(n).fill(0); for (let i = 0; i < n; i++) { arr[i] = i; } ``` This code creates an array of length `n` and fills it with incremental values. **Individual Test Cases** There are two test cases: 1. **Mutate**: This test case iterates over the array using a for loop, assigning each element to twice its original value. ```javascript for (let i = 0; i < arr.length; i++) { arr[i] = i * 2; } ``` Pros: Simple and straightforward. Cons: May not be efficient due to unnecessary copies of the array elements. 2. **Copy & Shift**: This test case creates a new copy of the array using `Array.prototype.shift()` and pushes the doubled value onto the new array. ```javascript const copy = []; for (let i = 0; i < arr.length; i++) { const item = arr.shift(); copy.push(item * 2); } ``` Pros: May be more efficient due to avoiding unnecessary copies of the original array elements. Cons: Requires two array operations, which may introduce additional overhead. **Library Usage** There is no explicit library usage in this benchmark. However, `Array.prototype.fill()` and `Array.prototype.shift()` are built-in JavaScript methods that are likely implemented by the browser or engine used for testing. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Other Alternatives** Some alternative approaches to mutating an array could include: * Using `Array.prototype.map()` and assigning the result back to the original array. * Creating a new array using `Array.from()` and pushing the doubled values onto it. * Using a custom implementation of array mutation, such as modifying the array's internal buffer. These alternatives may have different trade-offs in terms of performance, readability, and complexity. The benchmark is likely designed to favor the "Copy & Shift" approach due to its simplicity and potential efficiency gains.
Related benchmarks:
filling
for loop array caching with mutations
for loop array caching with mutations II
Array mutation VS creation
Comments
Confirm delete:
Do you really want to delete benchmark?