Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring vs split
(version: 0)
Compares slice, subst, substring and split to each other
Comparing performance of:
slice vs substr vs substring vs split
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'en-GB'
Tests:
slice
var result = example.slice(0,2)
substr
var result = example.substr(0,2)
substring
var result = example.substring(0,2)
split
var result = example.split('-', 1)[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
slice
substr
substring
split
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.1:latest
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. The benchmark compares four ways to extract a substring from a string: `slice`, `substr`, `substring`, and `split`. Each test case is represented by an object in the JSON array, which defines a specific test scenario. Here are the individual test cases with explanations: 1. **`slice`**: * Benchmark Definition: `var result = example.slice(0,2)` * This test uses the `slice()` method to extract a substring from `example`. The first argument (0) specifies the starting index, and the second argument (2) specifies the number of characters to extract. * No external libraries are used in this test case. 2. **`substr`**: * Benchmark Definition: `var result = example.substr(0,2)` * This test uses the `substr()` method to extract a substring from `example`. The first argument (0) specifies the starting index, and the second argument (2) specifies the number of characters to extract. * No external libraries are used in this test case. Note that `substr()` is an older method that was deprecated in modern JavaScript. It's still supported for backwards compatibility but not recommended for new code. 3. **`substring`**: * Benchmark Definition: `var result = example.substring(0,2)` * This test uses the `substring()` method to extract a substring from `example`. The first argument (0) specifies the starting index, and the second argument (2) specifies the end index. * No external libraries are used in this test case. Note that `substring()` is also an older method that's been replaced by more efficient methods like `slice()`. 4. **`split`**: * Benchmark Definition: `var result = example.split('-', 1)[0]` * This test uses the `split()` method to split the string `example` into substrings based on the hyphen character (-). The first argument is the separator, and the second argument (1) specifies that we want to get only one part of the resulting array. We then extract the first element (0) from the resulting array. * No external libraries are used in this test case. The results show that `substring` performs best on Chrome 80, followed by `slice`, then `substr`, and finally `split`. This suggests that while all methods can be used for string manipulation, `substring` is still a good choice when you need to extract a substring from the start or middle of a string. **Library usage**: None in this benchmark. All test cases rely on built-in JavaScript methods. **Special JS feature/syntax**: None explicitly mentioned. The test cases use standard JavaScript syntax and built-in methods. **Alternatives**: * For simple string manipulation, you can use `slice()`, `substring()`, or even a combination of indexing and concatenation. * If you need to split strings frequently, consider using a dedicated library like Lodash's `split()` method or the `split()` method from the `string-utils` library. * In modern JavaScript, you can also use template literals (backticks) with string interpolation for simpler string manipulation tasks. That concludes our exploration of this benchmark!
Related benchmarks:
slice vs substr vs substring new
slice vs substr vs substring 122459
Performance Test: substring vs substr vs slice vs split for date
slice vs substring (removing rightmost char)
Comments
Confirm delete:
Do you really want to delete benchmark?