Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
string characters split map vs for loop vs split foreach
(version: 0)
Benchmark.js
Comparing performance of:
split map vs for loop vs split foreach
Created:
3 years ago
by:
Guest
Go to the latest result
Tests:
split map
const string = 'asdf1234qwer'; const mapper = (char) => String.fromCharCode(char.charCodeAt(0)) string.split('').map(mapper).join('')
for loop
const string = 'asdf1234qwer'; const mapper = (char) => String.fromCharCode(char.charCodeAt(0)) let result = "" for(const char of string) { result += mapper(char) }
split foreach
const string = 'asdf1234qwer'; const mapper = (char) => String.fromCharCode(char.charCodeAt(0)) let result = '' string.split('').forEach(char => { result += mapper(char) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
split map
for loop
split foreach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:131.0) Gecko/131.0 Firefox/131.0
Browser/OS:
Firefox Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
for loop
1.5M/s
split map
1.5M/s
split foreach
1.3M/s
View exact numbers
Test name
Executions per second
✓
for loop
1,549,646 Ops/sec
split map
1,529,963 Ops/sec
split foreach
1,254,232 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches to process a string of characters: using `map`, `for loop`, and `forEach` with a custom function `mapper`. The goal is to determine which approach is the fastest. **Approaches Compared** 1. **Map**: This approach uses the `map()` method, which applies a given function to each element of an array (in this case, the characters in the string) and returns a new array. 2. **For Loop**: This approach uses a traditional `for` loop to iterate over the characters in the string and applies the custom `mapper` function to each character. 3. **ForEach**: This approach uses the `forEach()` method, which executes a given function for each element of an array (in this case, the characters in the string). **Pros and Cons** * **Map**: + Pros: Concise and readable code, efficient use of memory. + Cons: May be slower due to array creation and iteration. * **For Loop**: + Pros: Control over each iteration, can be faster for small arrays or complex logic. + Cons: More verbose code, less readable, and may lead to errors if not implemented correctly. * **ForEach**: + Pros: Easy to read and write, efficient use of memory. + Cons: May be slower due to the overhead of function calls. **Library Used** In this benchmark, no external libraries are used. However, the `String.fromCharCode()` method is used to convert each character code to its corresponding ASCII value. **Special JS Features/Syntax** No special JavaScript features or syntax are being used in these benchmarks. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few: * **Regular Expressions**: You can use regular expressions to process the string, which may be faster for certain tasks. * **Array.prototype.reduce()**: Another approach is to use `reduce()` method, which combines the results of an array processing function into a single output value. **Benchmark Preparation Code** The preparation code is empty in this case, as no setup or initialization is required for these benchmarks. The focus is solely on measuring the execution time of each approach. Now that we've gone through the benchmark, it's clear that this comparison aims to highlight the trade-offs between using `map`, `for loop`, and `forEach` with a custom function in JavaScript.
Related benchmarks
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Comments
Confirm delete:
Do you really want to delete benchmark?