Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring (with no end index) big string
(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:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for(let i = 0; i < 100_000; i++) { arr.push('aaaaaaa'); } var example = arr.join();
Tests:
slice
var result = example.slice(100_000)
substr
var result = example.substr(100_000)
substring
var result = example.substring(100_000)
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
8654750.0 Ops/sec
substr
7924827.5 Ops/sec
substring
9868322.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, the options being compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark defines a comparison between three JavaScript methods: `slice()`, `substr()`, and `substring()` when used without an end index. The script preparation code creates an array of 100,000 strings, joins them together into a single string (`example`), and then attempts to slice or substring this large string from the start. **Script Preparation Code** The script prepares an array of 100,000 strings by pushing 'aaaaaaa' into an array, which is then joined together using `join()`. ```javascript var arr = []; for(let i = 0; i < 100_000; i++) { arr.push('aaaaaaa'); } var example = arr.join(''); ``` **Options being compared** The benchmark compares the performance of three methods: 1. **slice()**: Returns a new string containing all characters from the start index to the end of the string. 2. **substr()**: Returns a substring starting at the specified index. 3. **substring()**: Returns a substring from the start index to the end index. **Pros and Cons** * **Slice()**: Pros: Simple, efficient. Cons: Can be slow if used with large strings because it creates a new string object. In this case, since we're using an end index of 100,000 (which is beyond the length of our `example` string), it will return an empty string. * **Substr()**: Pros: Similar to slice, but can be more efficient when the start index is close to the end of the string. Cons: Can still create a new string object and may not be as efficient as slice for large strings with small start indices. * **Substring()**: This method is less commonly used than `slice()` and `substr()`. However, it can be more efficient when both start and end indices are specified. **Library and Purpose** There is no library being used in this benchmark. The script directly uses built-in JavaScript methods (`join()`, `slice()`, `substr()`, and `substring()`). **Special JS Features or Syntax** There is no specific special feature or syntax being tested here, other than the behavior of these three built-in string methods when not using an end index. **Other Considerations** * **Memory Allocation**: Since we're creating a large array and then joining it together into a single string, there may be significant memory allocation going on. This could impact performance. * **Cache Locality**: Depending on the browser's cache policy, some browsers might exhibit different behavior in terms of caching or accessing memory locations for this benchmark. **Alternatives** If you wanted to explore alternatives, here are a few options: * **Use an existing library like Lodash**: While not strictly necessary, using a library like Lodash can often simplify and speed up code compared to implementing your own custom methods. * **Modify the test case to include more realistic inputs**: This could make the benchmarking process more useful for real-world scenarios where string slicing might be used with larger or more complex input strings. Keep in mind that while these alternatives can offer benefits, they may also introduce additional complexity and dependencies.
Related benchmarks:
slice vs substr vs substring (with end index) -x
slice vs substr vs substring (with no end index) - 2
Performance Test: substring vs substr vs slice constant length
array from string and slice or substring and array from shorter string
Performance Test: substring vs substr vs slice with StartIndex
Comments
Confirm delete:
Do you really want to delete benchmark?