Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Creating new property, 2
(version: 0)
Comparing performance of:
For vs Map vs Reduce vs Foreach vs For reversed vs while
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
data = []; for (let i = 0; i < 10000; i++) { data[i] = { prop: '1' }; }
Tests:
For
for (let i = 0; i < data.length; i++) { data[i].newProp = '1'; }
Map
data.map((current) => { current.newProp = '1'; });
Reduce
data.reduce((accumulator, current) => { accumulator.push({ ...current, newProp: '1' }); return accumulator; }, []);
Foreach
data.forEach((current) => { current.newProp = '1'; });
For reversed
for (let i = data.length - 1; i >= 0; i--) { data[i].newProp = '1'; }
while
let i = 0; while (i !== data.length) { data[i].newProp = '1'; i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
For
Map
Reduce
Foreach
For reversed
while
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what is being tested on the provided JSON that represents the benchmark. **Benchmark Definition** The test case measures how quickly different JavaScript methods can create and assign a new property to an existing object within an array. The goal is to compare the performance of various looping methods: `for`, `forEach`, `map`, `reduce`, and `while`. **Individual Test Cases** Here are the specific benchmark tests: 1. **For**: A traditional `for` loop iterates over the array, creating a new property on each object. 2. **Map**: The `map()` method is used to create a new array with the updated objects. 3. **Reduce**: The `reduce()` method is used to accumulate the updates into a single array. 4. **Foreach**: A `forEach()` loop iterates over the array, creating a new property on each object. 5. **For reversed**: A reverse iteration using a traditional `for` loop. 6. **While**: A while loop iterates over the array, creating a new property on each object. **JavaScript Features and Libraries** * No external libraries are used in these test cases. * The tests use JavaScript built-in methods such as `map()`, `reduce()`, `forEach()`, and traditional loops (`for` and `while`). * The spread operator (`...`) is used in the `Reduce` test case to create a new object with the updated property. **Pros and Cons of Different Approaches** The results show that: * **Foreach**: Performs reasonably well, but not as fast as other methods. * **Map**: Outperforms `forEach` by a significant margin due to its optimized iteration mechanism. * **Reduce**: While slower than `map`, it still performs better than the traditional loops (`for` and `while`) due to its ability to accumulate updates. * **For** (traditional loop): The slowest method, likely due to the overhead of explicit indexing and loop control. * **While**: Also slower than other methods, probably because of the unnecessary iteration from end to start. Other considerations: * The results are specific to Chrome 80 on Linux desktop. * The test case uses a relatively small array size (10,000 elements), which may not reflect real-world scenarios.
Related benchmarks:
Checking property existence
Creating new property, 1
Test of 2.1
assign vs direct
Comments
Confirm delete:
Do you really want to delete benchmark?