Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs substr
(version: 0)
Comparing performance of:
startsWith vs substr
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id=''></div>
Script Preparation code:
window.startswith = '![]'; 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) === true; 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, match) === '![]'; 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 JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmark named "startsWith vs substr". It defines two test cases: 1. `startsWith`: Compares the execution time of using `str.startsWith(match)` to check if a string starts with a certain substring. 2. `substr`: Compares the execution time of using `str.substr(0, match) === '![]'` to extract a substring from a given string. **Script Preparation Code** The script preparation code sets up some global variables: * `window.startswith = '![]';`: Defines a custom `startswith` function that always returns `true`. * `var data = window.data = [];`: Initializes an empty array `data` as a global variable. * `const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";`: Defines a string `possible` containing all ASCII characters (letters, numbers, and special characters). * `var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000;`: Sets the total number of strings to generate to 100,000. The script also defines two helper functions: * `getRandomInt(max) { return Math.floor(Math.random() * max); }`: Returns a random integer between 0 and `max-1`. * `makeRandomString(len) { ... }`: Generates a random string of length `len` by concatenating characters from the `possible` string. The script then generates an array of 100,000 random strings using `makeRandomString(getRandomInt(20))` and pushes them into the global `data` array. **Html Preparation Code** The HTML preparation code is a simple `<div id=''></div>` element that will be used as a container for the benchmark execution. **Individual Test Cases** Each test case defines a small script snippet that: 1. Initializes variables from the global scope (`window.TOTAL_STRINGS`, `window.data`, and `window.match`). 2. Loops through the generated data array using an index `x`. 3. For each string `str` at index `x`, checks if it starts with the given substring using either `startsWith(match)` or `substr(0, match) === '![]'`. **Benchmark Results** The latest benchmark result shows the execution time for both test cases on a specific browser and device platform: * `substr`: 5945.8876953125 executions per second. * `startsWith`: 576.456787109375 executions per second. Now, let's discuss the pros and cons of each approach: **`startsWith(match)`** Pros: * More readable code * Easier to understand for developers familiar with string methods Cons: * May be slower due to the overhead of string matching algorithms (e.g., KMP or Boyer-Moore) * Can lead to slower performance if the substring is large **`str.substr(0, match) === '![]'`** Pros: * Can be faster for small substrings * Avoids the overhead of string matching algorithms Cons: * Less readable code * May require additional checks to ensure `match` is within bounds Other alternatives to consider: * Using a more efficient string searching algorithm like Aho-Corasick or Suffix Tree. * Optimizing the `substr` approach by using a fixed-size substring instead of a variable-length one. Keep in mind that these optimizations might come with additional overhead, trade-offs between performance and readability.
Related benchmarks:
.startsWith vs .charAt for single character
.startsWith vs .charAt for single character v3
.startsWith vs .substr
.startsWith vs .charAt vs str[0] for single character
Comments
Confirm delete:
Do you really want to delete benchmark?