Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs split
(version: 0)
Comparing performance of:
for vs split
Created:
6 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var digits = '06325112733';
Tests:
for
let sum = 0; var digitsLength = digits.length; for (let i = 0; i < digitsLength; ++i) { var digit = +digits[i]; sum += digit * (digitsLength + 1 - i); } var lastSumChecker = sum % 11; var checker = lastSumChecker < 2 ? 0 : 11 - lastSumChecker; return checker;
split
let sum = 0; var digitsLength = digits.length; sum = digits.split('').reduce((prev, curr, index) => { var current = +curr; return prev + current * (digitsLength + 1 - index); }, 0); var lastSumChecker = sum % 11; var checker = lastSumChecker < 2 ? 0 : 11 - lastSumChecker; return checker;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
split
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
for
13.9M/s
split
11.8M/s
View exact numbers
Test name
Executions per second
✓
for
13,947,115 Ops/sec
split
11,787,033 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **What is being tested?** The benchmark is testing two approaches for calculating the sum of digits in a string, where each digit has a weight based on its position (from 0 to n). The approach is to compare the performance of two methods: 1. **For loop**: Using a traditional `for` loop to iterate over the characters in the string. 2. **Split and reduce**: Using the `split()` method to split the string into an array of characters, and then using the `reduce()` method to sum up the weighted digits. **Options comparison** The two approaches have different pros and cons: * **For loop**: + Pros: Simple and straightforward, easy to understand. + Cons: Can be slow for large strings due to the overhead of incrementing a counter and checking the bounds. * **Split and reduce**: + Pros: More efficient for large strings since `split()` is faster than iterating over each character individually, and `reduce()` avoids the need to manually keep track of indices. + Cons: Requires more memory and can be less intuitive due to the use of an array and a callback function. **Library usage** Neither benchmark uses any external libraries beyond what's included with JavaScript. However, it's worth noting that in a real-world scenario, you might consider using libraries like Lodash for its `reduce()` function or String.prototype.split() if performance matters. **Special JS feature/syntax** The benchmarks don't use any special JavaScript features or syntax, making them accessible to developers of all skill levels. **Other alternatives** If you were to write this benchmark from scratch, other approaches might include: * Using a single loop with arithmetic operations directly on the string (e.g., `for (let i = 0; i < digitsLength; ++i) { sum += +digits[i] * (digitsLength + 1 - i); }`) * Utilizing some CPU-specific instruction sets (like SIMD) for parallel processing, which might provide even better performance but would require more complex code and potentially less portability. However, these alternatives are likely to have a higher overhead or require more specialized knowledge, making the original "for vs split" approach a good starting point for benchmarking.
Related benchmarks
parseInt vs Number // toString vs String
Comments
Confirm delete:
Do you really want to delete benchmark?