Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
add fields to objects in array
(version: 0)
speed of different variants of adding field into objects in array
Comparing performance of:
map vs forEach vs for of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var objects = [{ a: 'Ghkfj', b: 'Yvtzq', c: 'Rwsde' }, { a: 'Bpqwm', b: 'Sdfgh', c: 'Zxcvb' }, { a: 'Olmnp', b: 'Iujkl', c: 'Qwerty' }, { a: 'Cdsaf', b: 'Asdfg', c: 'Xcvbn' }, { a: 'Qwerz', b: 'Cvbnm', c: 'Xcvbn' } ];
Tests:
map
objects = objects.map(el => ({ ...el, d: Math.random() }));
forEach
objects.forEach(el => el.d = Math.random());
for of
for (let el of objects) el.d = Math.random();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map
forEach
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map
796249.1 Ops/sec
forEach
1102060.8 Ops/sec
for of
1169637.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and individual test cases. **Benchmark Definition** The `Name` field indicates that this benchmark tests the speed of adding fields to objects in an array. The `Description` provides a brief summary of what the benchmark is checking. **Script Preparation Code** This section defines a JavaScript array `objects` with six objects, each containing three properties (`a`, `b`, and `c`). Each object has unique values for these properties. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only tests the JavaScript performance and does not involve any DOM-related operations. **Individual Test Cases** The benchmark consists of three test cases: 1. **map**: This test case uses the `map()` method to add a new property `d` with a random value to each object in the `objects` array. 2. **forEach**: This test case uses the `forEach()` method to iterate over the `objects` array and add a new property `d` with a random value to each object. 3. **for of**: This test case uses a traditional `for` loop to iterate over the `objects` array and add a new property `d` with a random value to each object. **Benchmark Results** The latest benchmark results show that: * The `map()` method is the fastest, with an execution rate of 1169637.125 executions per second. * The `forEach()` method is slower than `map()`, but still relatively fast, with an execution rate of 1102060.75 seconds. * The traditional `for` loop is the slowest, with an execution rate of 796249.0625 seconds. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **map()**: This method is efficient because it creates a new array with modified objects, which allows for parallel processing and caching. However, it may have higher overhead due to the creation of a new array. * **forEach():** This method iterates over the array synchronously, which can lead to slower performance compared to `map()`. However, it provides more control over the iteration process. * **for**: Traditional `for` loops are generally slower because they use a linear search for each element in the array. They also do not provide the same level of parallelism as `map()` or `forEach()`. **Library and Purpose** In this benchmark, there is no explicit library used. However, the `map()` and `forEach()` methods rely on the built-in JavaScript functions to operate on arrays. **Special JS Feature or Syntax** There are no special features or syntax used in this benchmark that would require specific knowledge of JavaScript beyond basic concepts. Now, regarding alternatives to MeasureThat.net: * **Benchmarking tools:** Other popular benchmarking tools for JavaScript include: + Benchmark.js + jsPerf + Microbenchmark (a command-line tool) * **Profiling tools:** Tools like Chrome DevTools or Node.js Inspector can provide more detailed information about performance issues and optimization opportunities. * **Testing frameworks:** Frameworks like Jest, Mocha, or Cypress can be used to write test cases for JavaScript applications, but they are not specifically designed for benchmarking. Keep in mind that MeasureThat.net is a specialized tool for benchmarking JavaScript performance, so it's essential to use it when you need to compare the speed of different JavaScript implementations or variations.
Related benchmarks:
Object speard vs assign
Object.keys vs Object.values
Increment test
Assignt vs spred
JavaScript, spread operator vs Object.assign vs property setter using array as source
Comments
Confirm delete:
Do you really want to delete benchmark?