Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript loops over string
(version: 0)
Comparing performance of:
for-in vs for-of vs for vs optimized-for
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
for-in
const items = 'abcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyz' let index = 0; for (let i in items) { index++; }
for-of
const items = 'abcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyz' let index = 0; for (let i of items) { index++; }
for
const items = 'abcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyz' let index = 0; for(let i = 0; i < items.length; ++i) { index++; }
optimized-for
const items = 'abcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmopqrstuvwxyz' let index = 0; for(let i = items.length; i > 0; --i) { index++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for-in
for-of
for
optimized-for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
118605.7 Ops/sec
for-of
1531239.4 Ops/sec
for
6949163.0 Ops/sec
optimized-for
6917485.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this case. **Benchmark Definition** The benchmark definition is a JSON object that describes the test to be performed. In this case, it's quite simple: * The name and description are empty. * There's no script preparation code or HTML preparation code provided, which means the test will run on an empty string. However, let's look at the individual test cases, which are the real meat of the benchmark. **Individual Test Cases** There are four test cases, each with a slightly different approach to iterating over a large string. Here they are: 1. **for-in** ```javascript for (let i in items) { index++; } ``` This is an old-school way of iterating over a string using the `in` operator. It's not the most efficient or modern way, but it works. 2. **for-of** ```javascript for (let i of items) { index++; } ``` This is a more modern way of iterating over a string using the `of` keyword. It's faster and more concise than the old `in` operator method. 3. **for** ```javascript for(let i = 0; i < items.length; ++i) { index++; } ``` This is a traditional `for` loop that uses the length property of the string to iterate over its characters. 4. **optimized-for** ```javascript for(let i = items.length; i > 0; --i) { index++; } ``` This is an optimized version of the traditional `for` loop, which starts from the end of the string and decrements the index. **Library and Special JS Features** There are no libraries mentioned in this benchmark definition. However, it's worth noting that some browsers may use their own internal optimizations or libraries to improve performance when running microbenchmarks. As for special JS features, there aren't any explicitly mentioned here. The benchmarks only focus on the iteration methods themselves. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: * **for-in**: Slowest due to the overhead of using `in` operator. Not recommended for performance-critical code. * **for-of**: Faster than `for-in`, but still slower than traditional `for` loops. Good balance between readability and performance. * **for**: Traditional method, can be fast if optimized correctly. May have some overhead due to checking the length property. * **optimized-for**: Optimized version of traditional `for` loop, should be faster than regular `for`. However, may not be as readable. **Other Alternatives** If you're looking for alternative ways to iterate over strings in JavaScript, here are a few options: * Using `Array.prototype.forEach()`: This method is similar to `for-of`, but returns no value. * Using `String.prototype.split()` and then iterating over the resulting array: This approach can be slower than traditional loops due to the overhead of creating an array. Keep in mind that these alternatives may not offer significant performance improvements for small strings or low-contention scenarios. However, they can provide more flexibility and readability in certain situations. **Conclusion** In conclusion, this benchmark definition is testing four different approaches to iterating over a large string in JavaScript: `for-in`, `for-of`, traditional `for` loop, and optimized `for` loop. The results show that the optimized `for` loop is the fastest, followed closely by the traditional `for` loop. While `for-of` and `for-in` are faster than their predecessors, they still trail behind the optimized `for` loop.
Related benchmarks:
Substring
for vs for..of String 1000
Loop Times
iterator speed
String indexer vs substring
Comments
Confirm delete:
Do you really want to delete benchmark?