Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring (average)
(version: 0)
Compares slice, substr and substring to each other when there is only a start index
Comparing performance of:
slice vs substr vs substring
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'there is no spoon'
Tests:
slice
var result = example.slice(10) var result = example.slice(0,5) var result = example.slice(0,-5)
substr
var result = example.substr(10) var result = example.substr(0,5) var result = example.substr(0,-5)
substring
var result = example.substring(10) var result = example.substring(0,5) var result = example.substring(0,-5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
substr
substring
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 dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. **Benchmark Overview** The provided benchmark compares three methods for extracting substrings from a given string: `slice()`, `substr()`, and `substring()`. **Comparison Options** Here are the three options being compared: 1. `slice(start, end)` 2. `substr(start)` 3. `substring(start)` Each option has its own pros and cons: * **`slice(start)`**: This method returns a new string containing all characters from `start` to the end of the original string. The "end" index is excluded. * Pros: Simple, fast, and efficient. * Cons: Requires two separate calls if you want to get the start substring (e.g., `example.slice(0, 5)`). * **`substr(start)`**: This method returns a new string containing all characters from `start` to the end of the original string. The "end" index is excluded. * Pros: Similar to `slice()`, but can be more readable in some cases (e.g., `example.substr(0, 5)`). * Cons: Also requires two separate calls if you want to get the start substring. * **`substring(start)`**: This method returns a new string containing all characters from `start` to `end-1`. The "end" index is inclusive. * Pros: More intuitive than `substr()`, as it uses a single call with two indices (e.g., `example.substring(0, 5)`). * Cons: Can be slower than `slice()` or `substr()` due to the additional character in the end. **Library and Special Features** None of these methods rely on any external libraries or special JavaScript features. The test is purely focused on comparing the performance of each method. **Other Alternatives** In modern JavaScript, you might consider using template literals or the `includes()` method for substring extraction. However, these alternatives are not being tested in this benchmark. Here's a brief explanation of how template literals and `includes()` work: * Template literals: `example.includes("substring")` returns true if "substring" is found in the string. It can be more readable than using one of the above methods for substring extraction. * Pros: More expressive, as it clearly conveys the intent of finding a substring. * Cons: Can be slower due to the extra overhead and potential regex-like behavior. * `includes()` method: This is not applicable in this case since we're working with substrings. **Benchmark Results** The latest benchmark results show that: 1. `substr()` has the highest executions per second (approximately 33 million). 2. `substring()` has a lower executions per second than `slice()`, but still relatively fast (around 33 million). 3. `slice()` has the lowest executions per second, likely due to its simplicity and the additional character in the end. Keep in mind that these results may vary depending on the specific use case and browser version. I hope this explanation helps you understand what's being tested in MeasureThat.net!
Related benchmarks:
slice vs substring
slice vs substr vs substring with end
slice vs substr vs substring (with end index) @fran
slice vs substring (with end index)
slice vs substr vs substrings
Comments
Confirm delete:
Do you really want to delete benchmark?