Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.startsWith vs .substr
(version: 0)
Comparing performance of: .startsWith vs .substr
Comparing performance of:
startsWith vs substr
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
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; while (x < TOTAL_STRINGS) { const str = data[x]; const str2 = data[getRandomInt(TOTAL_STRINGS)-1]; const res = str.startsWith(str2); x += 1; }
substr
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; while (x < TOTAL_STRINGS) { const str = data[x]; const str2 = data[getRandomInt(TOTAL_STRINGS-1)]; const res = str.substring(str2.length)===str2; 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
80.1 Ops/sec
substr
94.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark compares the performance of two string methods: `startsWith` and `substr`. The test creates an array of random strings with a fixed length (between 1 and 20) using a predefined character set. It then uses these arrays to perform matching operations between strings, measuring the execution time for each method. **Options being compared** The benchmark compares two approaches: 1. **`startsWith(str2)`**: This method checks if the original string (`str`) starts with the substring (`str2`). The test generates a random index `x` and uses it to access an element in the array, then checks if the first characters of that element match `str2`. 2. **`str.substring(str2.length) === str2`**: This method extracts a subset of characters from the original string (`str`) starting from the length of `str2`. It then compares this extracted substring with `str2`. The test generates a random index `x`, uses it to access an element in the array, and checks if the first characters of that element match `str2`. **Pros and Cons** * **`startsWith(str2)`**: Pros: + Simple and straightforward implementation. + Fast for most use cases. * Cons: + Can be slow when dealing with long strings or when `str2` is a prefix of multiple elements in the array. * **`str.substring(str2.length) === str2`**: Pros: + Efficient for matching prefixes across an entire array. * Cons: + More complex implementation due to substring extraction and comparison. **Library usage** There is no explicit library used in this benchmark, but it relies on the JavaScript `Array` and `String` prototypes, which are built-in and widely supported. **Special JS features or syntax** None of the test cases utilize any special JavaScript features or syntax beyond standard ECMAScript. **Other alternatives** If you wanted to optimize this benchmark, consider exploring other approaches: 1. **Caching**: Store pre-computed results for common prefix matches or substrings to avoid repeated computations. 2. **Parallelization**: Utilize multi-threading or parallel processing techniques to execute both methods concurrently and measure their performance simultaneously. 3. **Profiling**: Analyze the benchmark's execution time using profiling tools to identify performance bottlenecks and optimize hotspots. These alternatives can help you gain a deeper understanding of the performance characteristics of these string methods in different scenarios, but they would require additional effort and expertise. Keep in mind that this is just a starting point, and there are many other factors to consider when optimizing benchmark results.
Related benchmarks:
.startsWith vs .charAt for single character
.startsWith vs .charAt for single character v3
.startsWith vs .charAt vs str[0] for single character
startsWith vs substr
Comments
Confirm delete:
Do you really want to delete benchmark?