Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach, for, for-of, map
(version: 0)
Comparing performance of:
forEach() vs for of vs map() vs for
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const urls = [ 'http://localhost:3000/DEFAULT/allow/any', 'http://localhost:3000/DEFAULT/disallow/any', 'http://localhost:3000/DEFAULT/allow/file-type/*.png', 'http://localhost:3000/DEFAULT/allow/ended-url$', 'http://localhost:3000/DEFAULT/allow/query-string?id=rapid7/*.txt', 'http://localhost:3000/slurp/allow/any', 'http://localhost:3000/slurp/disallow/any', 'http://localhost:3000/msnbot/allow/any', 'http://localhost:3000/msnbot/disallow/any', 'http://localhost:3000/googlebot/allow/any', 'http://localhost:3000/googlebot/disallow/any' ];
Tests:
forEach()
let regexes = [/\/googlebot\/disallow\/any/, /slurp/]; let urls = [ 'http://localhost:3000/DEFAULT/allow/any', 'http://localhost:3000/DEFAULT/disallow/any', 'http://localhost:3000/DEFAULT/allow/file-type/*.png', 'http://localhost:3000/DEFAULT/allow/ended-url$', 'http://localhost:3000/DEFAULT/allow/query-string?id=rapid7/*.txt', 'http://localhost:3000/slurp/allow/any', 'http://localhost:3000/slurp/disallow/any', 'http://localhost:3000/msnbot/allow/any', 'http://localhost:3000/msnbot/disallow/any', 'http://localhost:3000/googlebot/allow/any', 'http://localhost:3000/googlebot/disallow/any' ]; urls.forEach((url) => { regexes.forEach((regex) => { if (regex.test(url)) { urls = urls.filter((item) => item !== url); } }); });
for of
let regexes = [/\/googlebot\/disallow\/any/, /slurp/]; let urls = [ 'http://localhost:3000/DEFAULT/allow/any', 'http://localhost:3000/DEFAULT/disallow/any', 'http://localhost:3000/DEFAULT/allow/file-type/*.png', 'http://localhost:3000/DEFAULT/allow/ended-url$', 'http://localhost:3000/DEFAULT/allow/query-string?id=rapid7/*.txt', 'http://localhost:3000/slurp/allow/any', 'http://localhost:3000/slurp/disallow/any', 'http://localhost:3000/msnbot/allow/any', 'http://localhost:3000/msnbot/disallow/any', 'http://localhost:3000/googlebot/allow/any', 'http://localhost:3000/googlebot/disallow/any' ]; for (const url of urls) { for (const regex of regexes) { if (regex.test(url)) { urls = urls.filter(item => item !== url) } } }
map()
let regexes = [/\/googlebot\/disallow\/any/, /slurp/]; let urls = [ 'http://localhost:3000/DEFAULT/allow/any', 'http://localhost:3000/DEFAULT/disallow/any', 'http://localhost:3000/DEFAULT/allow/file-type/*.png', 'http://localhost:3000/DEFAULT/allow/ended-url$', 'http://localhost:3000/DEFAULT/allow/query-string?id=rapid7/*.txt', 'http://localhost:3000/slurp/allow/any', 'http://localhost:3000/slurp/disallow/any', 'http://localhost:3000/msnbot/allow/any', 'http://localhost:3000/msnbot/disallow/any', 'http://localhost:3000/googlebot/allow/any', 'http://localhost:3000/googlebot/disallow/any' ]; urls.map(url => { regexes.map(regex => { if (regex.test(url)) { urls = urls.filter(item => item !== url) } }) })
for
let regexes = [/\/googlebot\/disallow\/any/, /slurp/]; let urls = [ 'http://localhost:3000/DEFAULT/allow/any', 'http://localhost:3000/DEFAULT/disallow/any', 'http://localhost:3000/DEFAULT/allow/file-type/*.png', 'http://localhost:3000/DEFAULT/allow/ended-url$', 'http://localhost:3000/DEFAULT/allow/query-string?id=rapid7/*.txt', 'http://localhost:3000/slurp/allow/any', 'http://localhost:3000/slurp/disallow/any', 'http://localhost:3000/msnbot/allow/any', 'http://localhost:3000/msnbot/disallow/any', 'http://localhost:3000/googlebot/allow/any', 'http://localhost:3000/googlebot/disallow/any' ]; for (let i = urls.length - 1; i >= 0; i--) { for (let j = regexes.length - 1; j >= 0; j--) { if (regexes[j].test(urls[i])) { urls = urls.filter(item => item !== urls[i]) } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach()
for of
map()
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach()
1147142.6 Ops/sec
for of
1160329.9 Ops/sec
map()
1039984.9 Ops/sec
for
1017532.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
It seems like we're about to dive into some performance benchmarking results! The results appear to be from a test suite that compares the performance of different JavaScript execution methods: `map()`, `forEach()`, and `for...of`. The tests are designed to measure how quickly these methods can filter out unwanted URLs from an array. Let's take a closer look at the top result for each method: 1. **`map()`**: Chrome 102, ExecutionsPerSecond = 600411.0 This suggests that the `map()` method is performing quite well in this test, likely due to its ability to use optimized Array.prototype methods and avoiding unnecessary iterations. 2. **`forEach()`**: Chrome 102, ExecutionsPerSecond = 597104.875 While not as fast as `map()`, the `forEach()` method still performs reasonably well. This might be because it's also utilizing optimized methods under the hood, but with a slightly higher overhead compared to `map()`. 3. **`for...of`**: Chrome 102, ExecutionsPerSecond = 592703.6875 The `for...of` loop is surprisingly fast! Its performance is likely due to its ability to use a more direct iteration approach, which avoids the overhead of Array.prototype methods. 4. **`for` ( traditional loop )**: Chrome 102, ExecutionsPerSecond = 571425.1875 This method performs slower than the others, mainly because it requires manual indexing and iteration. Keep in mind that these results might not be representative of your specific use case or codebase. The performance differences between these methods can vary depending on the context and implementation details. Which aspect of these benchmarking results would you like me to explore further?
Related benchmarks:
Simple Regex 3
New object every time
Test URLS/Regex Loop
regex vs startsWith vs indexOf perf on url
Comments
Confirm delete:
Do you really want to delete benchmark?