Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map/forEach arrow/function VS for let/var VS for const/let/var of
(version: 0)
Comparing performance of:
map arrow vs map function vs forEach arrow vs forEach function vs for let vs for var vs for const of vs for let of vs for var of
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...Array(10000).keys()]; var a;
Tests:
map arrow
a = 0; arr.map(x => { a += x + 2; }) console.log(a);
map function
a = 0; arr.map(function(x) { a += x + 2; }) console.log(a);
forEach arrow
a = 0; arr.forEach(x => { a += x + 2; }) console.log(a);
forEach function
a = 0; arr.forEach(function(x) { a += x + 2; }) console.log(a);
for let
a = 0; for (let i = 0; i < 10000; i++) { a += arr[i] + 2; } console.log(a);
for var
a = 0; for (var i = 0; i < 10000; i++) { a += arr[i] + 2; } console.log(a);
for const of
a = 0; for (const x of arr) { a += x + 2; } console.log(a);
for let of
a = 0; for (let x of arr) { a += x + 2; } console.log(a);
for var of
a = 0; for (var x of arr) { a += x + 2; } console.log(a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
map arrow
map function
forEach arrow
forEach function
for let
for var
for const of
for let of
for var of
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.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The test case compares four different ways to modify variables within loops: 1. Map arrow function (`map` method with arrow function) 2. Map traditional function (`map` method with traditional function) 3. For-each arrow function (`forEach` method with arrow function) 4. For-each traditional function (`forEach` method with traditional function) 5. For loop with `let`, `var`, and `const` 6. Traditional for loop **What's being tested** The test measures the performance of each approach in terms of executions per second (ExecutionsPerSecond) on a desktop browser running Firefox 99. **Comparison options** 1. **Map arrow function (`map` method with arrow function)**: This approach uses an arrow function to iterate over the array and modify the variable `a`. 2. **Map traditional function (`map` method with traditional function)**: This approach uses a traditional function ( anonymous function ) to iterate over the array and modify the variable `a`. 3. **For-each arrow function (`forEach` method with arrow function)**: This approach uses an arrow function to iterate over the array using the `forEach` method and modify the variable `a`. 4. **For-each traditional function (`forEach` method with traditional function)**: This approach uses a traditional function ( anonymous function ) to iterate over the array using the `forEach` method and modify the variable `a`. 5. **For loop with `let`, `var`, and `const**`: These are three ways to declare variables in a for loop, each with their own scope rules. 6. **Traditional for loop**: A classic for loop that uses a traditional `for` statement to iterate over the array. **Pros/Cons of each approach** 1. **Map arrow function**: * Pros: concise and expressive code * Cons: limited control over variable scope and behavior 2. **Map traditional function**: * Pros: more control over variable scope and behavior, but more verbose code * Cons: less readable and maintainable than arrow functions 3. **For-each arrow function**: * Pros: concise and expressive code, similar to map arrow function * Cons: limited control over variable scope and behavior 4. **For-each traditional function**: * Pros: more control over variable scope and behavior, but more verbose code * Cons: less readable and maintainable than arrow functions 5. **For loop with `let`, `var`, and `const`**: * Pros: more control over variable scope and behavior, similar to traditional for loops * Cons: more verbose code and potentially slower performance due to overhead of declaring variables 6. **Traditional for loop**: * Pros: simple and familiar syntax, easy to read and maintain * Cons: less concise than other approaches and may lead to errors due to scope rules The benchmark results show that the map arrow function approach is generally the fastest, followed by the traditional function approach, and then the for-each arrow function. The for loop with `let`, `var`, and `const` approaches are slower due to the overhead of declaring variables. The traditional for loop approach is the slowest due to its lack of optimization and potential scope-related errors. Keep in mind that this benchmark is specific to JavaScript and may not be representative of other programming languages or use cases.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Array.forEach vs Object.keys().forEach
Map.prototype.forEach vs Array.prototype.forEach
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?