Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS startsWith vs split upgraded
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
startsWith vs split
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'self:'; window.matchLength = window.match.length; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.startsWith(match) === 0; x += 1; }
split
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var length = window.matchLength; while (x < TOTAL_STRINGS) { const str = data[x]; str.split(':')[0] === match; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
split
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
1179.1 Ops/sec
split
95.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark and explain what's being tested. **Benchmark Overview** MeasureThat.net is comparing the performance of two string manipulation methods: `startsWith` and `split`. The benchmark uses a dataset of 100,000 randomly generated strings to test both approaches. **What's Being Tested?** The benchmark tests whether a given string starts with a specific prefix (`startsWith`) or if the first element of a split string is equal to a target value (`split`). Both tests use the same dataset and target values. **Options Compared:** 1. `startsWith`: This method checks if a given string starts with a specified prefix. 2. `split`: This method splits a string into an array of substrings using a specified separator (in this case, a colon (:)). **Pros and Cons of Each Approach:** 1. **`startsWith`:** * Pros: + Efficient for exact matches + Can use optimized algorithms like the `String.prototype.startsWith()` method in modern browsers * Cons: + May be slower for approximate matches or large datasets due to the need for a prefix match 2. **`split`:** * Pros: + Can handle partial matches and larger datasets efficiently, as it only needs to split once * Cons: + May have higher overhead due to string splitting and array creation **Library Usage** The benchmark uses no external libraries beyond the standard JavaScript `String` prototype methods. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being used in this benchmark. The code only employs standard JavaScript features like loops, functions, and string manipulation methods. **Alternative Approaches** Other alternatives to consider for similar string manipulation tasks might include: 1. **Regular Expressions (regex)**: Can be used for both `startsWith` and `split` operations, offering more flexibility but also potential performance overhead. 2. **String.prototype.includes()**: Similar to `startsWith`, but can handle any character match instead of just a prefix. 3. **Substring extraction methods**: Such as `substr()` or `slice()`, which might offer better performance for specific use cases. Keep in mind that the choice of approach ultimately depends on the specific requirements and constraints of your project.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS startsWith vs split
Comments
Confirm delete:
Do you really want to delete benchmark?