Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substring vs substr on 10 chars
(version: 0)
Comparing performance of:
slice vs Substring vs substr
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "0123456789";
Tests:
slice
var n1 = s1.slice(0, 6); var n2 = s1.slice(6);
Substring
var n1 = s1.substring(0, 6); var n2 = s1.substring(6, 10);
substr
var n1 = s1.substr(0, 6); var n2 = s1.substr(6, 10);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
Substring
substr
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
gemma2:9b
, generated one year ago):
This benchmark compares three different ways to extract substrings from a string in JavaScript: `slice()`, `substring()`, and `substr()`. **Here's a breakdown:** * **`slice()`**: This method creates a new string by taking a portion of the original string, specified by a start index and an optional end index. In this benchmark, it's used to extract the first six characters and then the remaining characters after index 6. * **`substring()`**: Similar to `slice()`, this method also creates a new string from a portion of the original string. It takes two parameters: the start index and the end index (exclusive). In this case, it's used for the same extractions as `slice()`. * **`substr()`**: This method is similar to `substring()` but only takes two parameters: the starting index and the length of the substring to extract. It also creates a new string. **Pros and Cons:** * **`slice()`**: Generally considered the most versatile due to its flexibility with start and end indices, handling negative indices, and returning an empty string when provided with out-of-bounds values. * **`substring()`**: Simpler syntax for extracting substrings within a defined range. * **`substr()`**: Can be more concise when you only need to extract a specific length of substring from a given starting point. **Considerations:** The benchmark focuses on raw execution speed, which is important for performance-critical applications. However, other factors like readability and maintainability also matter. In many cases, `slice()` might be the preferred choice due to its flexibility and common usage in JavaScript code. **Alternatives:** * For extracting characters individually, consider using a loop with string access (e.g., `s1[i]`). While potentially slower for large strings, it offers more granular control. * Modern JavaScript frameworks often provide optimized string manipulation methods tailored to their specific use cases. Let me know if you have any other questions!
Related benchmarks:
Performance Test: substring vs substr vs slice (remove last char)
slice vs substring remove last char
Performance Test: substring vs substr vs slice constant length
Performance Test: substring vs substr vs slice 1
slice vs substr vs substring 122459
Comments
Confirm delete:
Do you really want to delete benchmark?