Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string character comparison for...of vs for loop
(version: 0)
Testing different between loops
Comparing performance of:
for loop vs for of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = '' for(let i = 0;i<10000; i++) { str+=(Math.random()<.5?'a':'b') } var deleted = 0
Tests:
for loop
for(let i=0;i<str.length;i++){ if(str[i] === str[i+1]) deleted++ } return deleted
for of
var last = '' for(let i of str){ if(i === last) deleted++ last = i } return deleted
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.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance difference between two approaches: traditional `for` loops and the newer `for...of` loop syntax. Both loops are used to compare each character in a string with its adjacent characters, incrementing a counter if a match is found. **Script Preparation Code** The script preparation code generates a string of 10,000 random characters (either 'a' or 'b') using the `Math.random()` function. This creates a large, seemingly random dataset that's perfect for testing performance differences between loops. **Options Compared** Two options are being compared: 1. **Traditional `for` loop**: A traditional `for` loop is used to iterate over the characters in the string. 2. **`for...of` loop**: The newer `for...of` loop syntax is used, which is a more concise and expressive way of iterating over arrays or iterables. **Pros and Cons** Here are some pros and cons of each approach: Traditional `for` loop: Pros: * Widely supported across older browsers * Easy to understand for developers familiar with traditional loops Cons: * Can be verbose, especially when dealing with large datasets * May have slower performance compared to `for...of` `for...of` loop: Pros: * More concise and expressive than traditional `for` loops * Better suited for iterating over arrays or iterables Cons: * Less widely supported across older browsers (although still supported in modern versions) * Can be less intuitive for developers unfamiliar with the syntax **Library Used** In both benchmark scripts, the `deleted` variable is used to store the count of consecutive matches. There's no explicit library used here, as it's just a simple counter. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark, such as async/await, promises, or arrow functions. The focus is solely on comparing the performance of two iteration approaches. **Other Alternatives** If you're interested in exploring other microbenchmarking options, here are a few alternatives: 1. **Benchmark.js**: A lightweight JavaScript library for writing and running benchmarks. 2. **Benchmarklib**: A benchmarking framework that provides a simple way to write and compare benchmarks. 3. **Google Benchmark**: A high-performance benchmarking tool designed specifically for testing performance-critical code. Remember, when choosing a microbenchmarking tool or approach, consider factors such as accuracy, reliability, and the specific use case you're trying to measure.
Related benchmarks:
random String for loop vs while loop
Compare TextEncoder, Blob, new TextEncoder
Encode vs Blob vs length
Blob vs TextEncoder
Encode vs Blobie
Comments
Confirm delete:
Do you really want to delete benchmark?