Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs slice
(version: 0)
Comparing performance of:
regex vs slice vs while loop vs binary search
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const s = 'b'.repeat(22) + '0'.repeat(10);
Tests:
regex
const s = 'bbbbbbbbbbbbbbbbbbbbbb0000000000'; const re = new RegExp('0+$'); const res = s.replace('re', '');
slice
const s = 'bbbbbbbbbbbbbbbbbbbbbb0000000000'; const idx = s.indexOf(0); const res = s.slice(0, i);
while loop
const s = 'bbbbbbbbbbbbbbbbbbbbbb0000000000'; let idx = s.length - 1; while (idx > 0) { if (s[idx] === '0') { idx--; } else { break; } } const res = s.slice(0, i + 1);
binary search
const s = 'bbbbbbbbbbbbbbbbbbbbbb0000000000'; let l = 0; let r = s.length - 1; let mid; while (l < r) { mid = (l + r) >> 1; if (s[mid] === '0') { r = mid; } else { l = mid + 1; } } const res = s.slice(0, r);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex
slice
while loop
binary search
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing four approaches to find the first occurrence of '0' in a string: 1. Using `RegExp` (regex) 2. Using `indexOf` 3. Using a while loop 4. Using binary search **Script Preparation Code** The script prepares a string `s` by repeating the character `'b'` 22 times and appending `'0'` 10 times. **Benchmark Test Cases** Each test case is defined in a separate object, which includes: * `Benchmark Definition`: The code that performs the search * `Test Name`: A brief description of the approach being tested Let's analyze each test case: 1. **regex**: Uses a regular expression to find the first occurrence of '0' in the string. * Pros: Fast and efficient for finding a single occurrence, handles edge cases well. * Cons: Can be slower for longer strings due to the overhead of compiling the regex pattern. 2. **indexOf**: Uses the `indexOf` method to find the index of the first occurrence of '0' in the string. * Pros: Fast and efficient, suitable for most use cases. * Cons: May not handle edge cases well (e.g., empty string), and may return -1 if '0' is not found. 3. **while loop**: Uses a while loop to iterate through the string and find the first occurrence of '0'. * Pros: Easy to understand, suitable for educational purposes or simple cases. * Cons: Can be slow and inefficient for longer strings due to the overhead of looping. 4. **binary search**: Uses binary search to find the index of the first occurrence of '0' in the string. * Pros: Fast and efficient for finding a single occurrence, suitable for large datasets. * Cons: Requires sorting the string beforehand, which can be memory-intensive. **Library Used** None explicitly mentioned in this benchmark. However, it's likely that the `RegExp` library is used implicitly by the JavaScript engine. **Special JS Features or Syntax** None mentioned in this benchmark. **Other Alternatives** If you want to find the first occurrence of '0' in a string, other alternatives include: * Using `String.prototype.indexOf()` with a callback function * Using `Array.prototype.findIndex()` (if the string is converted to an array) * Using a custom implementation using bitwise operations Note that these alternatives may have different performance characteristics and are not necessarily more efficient than the approaches tested in this benchmark.
Related benchmarks:
test substring vs slice vs substr
substring vs slice v0.1
slice vs substring remove last char
slice vs substr vs substring new
slice vs substr vs substring 122459
Comments
Confirm delete:
Do you really want to delete benchmark?