Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript slice vs substring
(version: 0)
Comparing performance of:
Substring vs Slice vs Substr
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var text = '$test';
Tests:
Substring
text = text.substring(1, text.length);
Slice
text = text.slice(1, text.length);
Substr
text = text.substr(1, text.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Substring
Slice
Substr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Substring
10401095.0 Ops/sec
Slice
10619444.0 Ops/sec
Substr
10283436.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided JSON defines two benchmark tests: `Javascript slice vs substring`. The goal is to compare the performance of three different methods for extracting substrings from a string in JavaScript: 1. `Substring` (using `substring()` method) 2. `Slice` (using `slice()` method) 3. `Substr` (using `substr()` method) **Options Compared** The benchmark compares the execution time of each method on a test string. The options being compared are: * `substring()`: extracts a substring starting from index 1 to the end of the string. * `slice()`: extracts a slice of the string starting from index 1 to the end of the string. * `substr()`: extracts a substring starting from index 1 to the length of the string. **Pros and Cons** Here's a brief summary of the pros and cons of each method: * **Substring`**: + Pros: widely supported, simple to use. + Cons: may be slower than other methods due to its iterative approach. * **Slice`**: + Pros: efficient, uses `length` property for bounds, which can lead to better performance. + Cons: less widely supported than `substring()`, may not work in older browsers. * **Substr`**: + Pros: short and sweet, easy to read, but less efficient than other methods. **Library and Purpose** In the provided test case, none of the methods use a library. However, it's worth noting that all three methods are part of the standard JavaScript API. **Special JS Feature or Syntax** The benchmark uses special JavaScript feature/syntax: * The `slice()` method was introduced in ECMAScript 5 (ES5), released in 2009. * The `substr()` method is an older, deprecated method that has been replaced by `substring()`. * The use of `$test` as a variable name in the script preparation code is likely just a placeholder or a convention used by MeasureThat.net. **Other Alternatives** If you're looking for alternative methods to extract substrings from a string, here are a few options: * Use regular expressions: `text.match(/a-z/g)` would extract all lowercase letters from the string. * Use `split()` and `join()`: `text.split('a').join('')` would remove all occurrences of 'a' from the string. * Use a custom implementation using `for` loop or `Array.prototype.forEach()` Keep in mind that these alternatives may have different performance characteristics compared to the methods tested in this benchmark.
Related benchmarks:
slice vs substring from end
Last char remove V1.0
JS substring vs slice
_substring vs slice
Comments
Confirm delete:
Do you really want to delete benchmark?