Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.substr vs .startwith
(version: 0)
Testing some things
Comparing performance of:
startsWith vs substr
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^QG/; window.match = 'test'; var data = window.data = []; const possible = "0123456789QTPG"; 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(10))); }
Tests:
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; str.startsWith("QG"); x += 1; }
substr
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.substr(0,2)=="QG" x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
substr
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 break down the provided benchmark and its components. **Benchmark Overview** The benchmark compares two string comparison methods: `substr` and `startsWith`. The goal is to determine which method is faster. **Script Preparation Code** The script preparation code sets up a few variables: 1. `window.regex`: defines a regular expression (`/^QG/`) that matches the string "QG". 2. `window.match`: assigns the value "test" to this variable. 3. Creates an array `data` with 100,000 random strings (generated using `makeRandomString(len)`). 4. Defines a helper function `getRandomInt(max)` for generating random integers. **Html Preparation Code** The HTML preparation code is empty, as this benchmark only tests JavaScript code. **Individual Test Cases** There are two test cases: 1. **startsWith**: uses the `startsWith` method to check if each string in the `data` array starts with "QG". 2. **substr**: uses the `substr` method (with a fixed length of 2) to check if each string in the `data` array contains "QG" as its first two characters. **Library and Special JS Features** There is no explicit mention of any libraries, but `window.regex` suggests that this benchmark might use regular expressions. However, without more context or a deeper understanding of the script, it's difficult to confirm this. **Pros and Cons of Different Approaches** 1. **startsWith**: * Pros: more readable and efficient for exact matching. * Cons: may be slower if not implemented correctly (e.g., using `indexOf` instead of `startsWith`). 2. **substr**: * Pros: can be faster if optimized correctly, especially when the substring length is fixed. * Cons: less readable and might require more CPU cycles to compute the index. **Other Considerations** When evaluating these test cases, keep in mind that: 1. The benchmark measures the number of executions per second (ExecutionsPerSecond), which indicates how many iterations can be completed within a given time frame. 2. The results may vary depending on factors like JavaScript engine, browser version, and system resources. **Alternatives** Other string comparison methods you might consider testing in a similar benchmark include: 1. `indexOf()` 2. `includes()` (introduced in ECMAScript 2015) 3. Regular expression matching (`/regex/`) To make the benchmark more comprehensive, you could also explore additional factors, such as: 1. String length and complexity 2. Unicode support and compatibility 3. Performance variations across different browser engines
Related benchmarks:
.startsWith vs .substr
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
.startsWith vs .charAt vs str[0] for single character
startsWith vs substr
Comments
Confirm delete:
Do you really want to delete benchmark?