Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For Loop vs For Of in iterating strings
(version: 0)
validCharForL() and validCharForOf() iterate through a string and full-width any characters invalid for Windows and Linux, using, respectively, a for loop and a for of loop.
Comparing performance of:
For Loop vs For Of
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function validCharForL(s){ const n='"*/:<>?\\|'; let r=""; for(let i=0,l=s.length;i<l;i++) r+=n.includes(s[i])?String.fromCharCode(s.charCodeAt(i)+65248):s[i]; return r }; function validCharForOf(s){ const n='"*/:<>?\\|'; let r=""; for(let c of s) r+=n.includes(c)?String.fromCharCode(c.charCodeAt()+65248):c; return r };
Tests:
For Loop
for (let i = 0; i < 1000; i++) validCharForL("1234*/:<abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234");
For Of
for (let i = 0; i < 1000; i++) validCharForOf("1234*/:<abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For Loop
For 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.1:latest
, generated one year ago):
Let's break down the benchmark definition and results. **Benchmark Definition:** The benchmark tests two different approaches for iterating through a string and replacing invalid characters for Windows and Linux: 1. **`for` loop**: The `validCharForL()` function uses a traditional `for` loop to iterate through each character of the input string, checking if it's an invalid character using the `includes()` method. If it is, the function converts it to a valid character by adding 65248 to its Unicode code point. 2. **`for...of` loop**: The `validCharForOf()` function uses an ES6-style `for...of` loop (introduced in ECMAScript 2015) to iterate through each character of the input string. It then checks if the character is invalid using the same method as above. **Test Cases:** Two test cases are provided: 1. **For Loop**: This test case runs the `validCharForL()` function 1000 times with a large input string. 2. **For Of**: This test case runs the `validCharForOf()` function 1000 times with the same large input string. **Benchmark Results:** The results show that: 1. **`for` loop**: On Chrome 107 (Linux desktop), this approach executes approximately 322.7 times per second. 2. **`for...of` loop**: On the same environment, this approach executes approximately 288.4 times per second. **Library/Feature used:** No external libraries are used in these test cases. However: * The `includes()` method is part of the JavaScript standard library and checks if a character is present in a string. * The `String.fromCharCode()` method is also part of the JavaScript standard library and converts an ASCII value to its corresponding Unicode character. **Pro/Cons of each approach:** Both approaches are relatively simple and efficient for small input strings. However: * **`for` loop**: This approach has been around since ECMAScript 1 (1997) and might be more familiar to developers who have worked with older JavaScript versions. * **`for...of` loop**: Introduced in ECMAScript 2015, this feature is more concise and expressive, making it easier to read and write. However, some older browsers or environments might not support it. **Other Considerations:** When choosing between these approaches: * If performance is critical, the traditional `for` loop might be slightly faster due to its simplicity. * For larger input strings or complex iterations, a more modern approach like `for...of` or other asynchronous iteration methods (e.g., `Promise.all()`) might be more suitable. **Alternatives:** Other alternatives for iterating through strings include: * **Array.prototype.map()**: Convert each character to an array element and process it using the map method. * **Async/Await**: Use promises and async/await syntax to iterate through a string asynchronously. * **Modern iteration methods**: Utilize newer iteration methods like `String.prototype.split()` or `Array.prototype.reduce()`.
Related benchmarks:
cycle vs regex
Alphanumeric String
alpha-numeric string
reduce vs forEach vs for loop on a string
Comments
Confirm delete:
Do you really want to delete benchmark?