Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: slice vs substring vs substr vs subscript
(version: 0)
Comparing performance of:
slice vs substring vs substr vs subscript
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "I am the god of hellfire, and I bring you..."
Tests:
slice
var substring = string.slice(-1);
substring
var substring = string.substring(string.length - 1);
substr
var substring = string.substr(-1);
subscript
var substring = string[string.length - 1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
slice
substring
substr
subscript
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):
**Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares the performance of four different methods for extracting the last character from a string: `slice`, `substring`, `substr`, and subscript. **Methods being compared** 1. **`slice()`**: The `slice()` method returns a new string containing only the characters after the specified start index (in this case, `-1`, which means the end of the string). 2. **`substring()`**: The `substring()` method returns a new string containing only the characters from the specified start index to the end of the string (also `-1` in this case). 3. **`substr()`**: The `substr()` method is similar to `substring()`, but it requires specifying both the start and end indices. 4. **Subscript** (not a standard JavaScript method): This approach uses array indexing (`string[string.length - 1]`) to extract the last character. **Pros and Cons** * **`slice()`**: Pros: simple, efficient, and widely supported. Cons: may be slower than other methods for very large strings. * **`substring()`**: Pros: similar to `slice()`, but allows specifying a start index. Cons: slightly slower than `slice()` due to the additional calculation. * **`substr()`**: Pros: faster than `substring()` for very large strings, as it only requires two indices calculations. Cons: less flexible than `substring()`. * **Subscript**: Pros: simple and straightforward. Cons: not a standard JavaScript method, which might make it less readable or maintainable. **Library usage** None of the methods being compared rely on any external libraries. They are all built-in to JavaScript. **Special JS feature or syntax** The `slice()`, `substring()`, and `substr()` methods use array indexing and string manipulation, which is a standard feature of JavaScript. The subscript method uses array indexing, which is also a standard feature but not directly related to strings. **Other alternatives** For extracting the last character from a string, other alternatives include: * Using a regular expression with `RegExp.prototype.exec()` or `String.prototype.match()`. * Converting the string to an array using `Array.from(string)` and then accessing the last element. * Using a library like Lodash's `last` function. However, for most use cases, the methods being compared in this benchmark (e.g., `slice()`, `substring()`, and `substr()`) are sufficient.
Related benchmarks:
Performance Test: substring vs substr vs slice
Performance Test: substring vs subsstr vs slice
Performance Test: substring vs substr vs slice constant length
Performance Test: substring vs substr (remove last 10 chars)
JS substring vs slice
Comments
Confirm delete:
Do you really want to delete benchmark?