Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS startsWith vs split
(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:
3 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 = 'test'; 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["-"] === 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:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
845.0 Ops/sec
split
13408.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Overview** This benchmark is comparing the performance of two string manipulation methods: `startsWith` and `split`. The test case generates a large dataset of random strings and checks which method is faster to execute. **Benchmark Definition JSON** The benchmark definition defines two test cases: 1. "startsWith" 2. "split" Each test case has its own script preparation code, which sets up the variables and data needed for the test. ```json "Script Preparation Code": "window.regexStart = /^test/;\r\nwindow.regexEnd = /test$/;\r\nwindow.match = 'test';\r\nwindow.matchLength = window.match.length;\r\nvar data = window.data = [];\r\nconst possible = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\r\nvar TOTAL_STRINGS = window.TOTAL_STRINGS = 100000;\r\n" ``` The script preparation code defines two regular expressions: `regexStart` and `regexEnd`. It also sets up an array of random strings (`data`) and a variable `match`. **Individual Test Cases** Each test case has its own benchmark definition: ```json { "Benchmark Definition": "var x = 0;\r\nvar TOTAL_STRINGS = window.TOTAL_STRINGS;\r\nvar data = window.data;\r\nvar match = window.match;\r\nwhile (x < TOTAL_STRINGS) {\r\n const str = data[x];\r\n str.startsWith(match) === 0;\r\n x += 1;\r\n}", "Test Name": "startsWith" } ``` ```json { "Benchmark Definition": "var x = 0;\r\nvar TOTAL_STRINGS = window.TOTAL_STRINGS;\r\nvar data = window.data;\r\nvar match = window.match;\r\nvar length = window.matchLength;\r\nwhile (x < TOTAL_STRINGS) {\r\n const str = data[x];\r\n str.split[\"-\"] === match;\r\n x += 1;\r\n}", "Test Name": "split" } ``` Each test case uses the `window` object to access the variables and data defined in the script preparation code. The test cases are executed a large number of times (defined by the `TOTAL_STRINGS` variable). **Library and Special JS Features** The benchmark uses two libraries: 1. **None**: The benchmark does not use any external libraries. 2. **Math.random()**: The benchmark uses the `Math.random()` function to generate random numbers. There are no special JavaScript features or syntax used in this benchmark. **Pros and Cons of Approaches** Here's a brief overview of the two approaches being compared: 1. **startsWith** * Pros: + More efficient for matching strings that start with a specific pattern. + Can be optimized using regular expressions. * Cons: + May not work as expected if the string is null or undefined. 2. **split** * Pros: + Works well for splitting strings into substrings using a delimiter. + Can be used to split strings at specific positions. * Cons: + May not be as efficient as `startsWith` for matching strings that start with a specific pattern. **Other Alternatives** Other alternatives to the `startsWith` and `split` methods include: 1. **Substr**: Instead of using `split`, you could use `substr` to extract substrings from a string. 2. **IndexOf**: Another method for finding the position of a substring within a string is `indexOf`. 3. **Regexp.test**: You could also use the `test()` method of regular expressions to match strings against a pattern. These alternatives may have different performance characteristics and use cases, depending on the specific requirements of your application.
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 upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?