Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs substr vs slice vs split
(version: 0)
Comparing performance of:
slice vs substring vs substr vs split
Created:
5 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(17, 25);
substring
var substring = string.substring(17, 25);
substr
var substring = string.substr(17, 25);
split
var substring = string.split(',')[1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
slice
substring
substr
split
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
180157120.0 Ops/sec
substring
161157232.0 Ops/sec
substr
173935008.0 Ops/sec
split
43169012.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's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of four string manipulation methods: 1. `substring()` 2. `substr()` 3. `slice()` 4. `split()` (specifically, splitting a comma-separated string) **Options Compared** Each test case compares two options for creating a substring or split string. * For `substring()`, the comparison is between `string.substring(17, 25)` and `string.slice(17, 25)`. * For `substr()`, the comparison is between `string.substr(17, 25)` and `string.slice(17, 25)`. * For `split()`, the comparison is between `string.split(',')[1]` and no alternative (i.e., just using `string` as a single value). **Pros and Cons of Different Approaches** Here's a brief overview of each approach: * **`substring()`**: This method returns a new string containing a section of the original string. It's generally considered the most readable and maintainable option among the four. * Pros: Easy to read, clear purpose, efficient for small substrings. * Cons: Can be slower than other methods for large substrings. * **`substr()`**: This method is similar to `substring()`, but it's less commonly used in modern JavaScript. It returns a new string containing a section of the original string, starting from the offset specified by the second argument. * Pros: Similar to `substring()`, but can be slightly faster due to its native implementation. * Cons: Less readable and maintainable than `substring()`, less commonly used in modern JavaScript. * **`slice()`**: This method returns a new array containing a section of the original string. When used with two arguments, it returns an array containing one element (a substring). It's generally faster than `substring()` for large substrings. * Pros: Fast, efficient for large substrings, flexible in its usage (can be used as an array or string). * Cons: Less readable and maintainable than `substring()`, can be confusing when used with arrays. * **`split()`**: This method splits a string into an array of substrings based on a specified separator. When using only one argument, it returns the entire string if it doesn't contain the separator. * Pros: Fast and efficient for splitting strings. * Cons: Not suitable for extracting substrings from a single string. **Library** The `split()` method uses the built-in JavaScript `String.prototype.split()` method. This method is implemented in native code, making it fast and efficient. **Special JS Feature/Syntax** There are no special features or syntaxes used in this benchmark that would require additional explanation. **Other Considerations** When choosing between these string manipulation methods, consider the following factors: * **Readability**: Choose `substring()` for its clear purpose and readability. * **Performance**: Use `slice()` for large substrings due to its native implementation and efficiency. * **Flexibility**: Consider using `split()` when working with comma-separated strings or arrays. **Alternatives** Some alternative string manipulation methods you might consider include: * **Regular expressions (RegExp)**: Useful for complex pattern matching, but can be slower than the other methods for simple substring extraction. * **Array methods**: Such as `slice()`, `substr()`, and `indexOf()`, which can be useful when working with arrays or strings in a more functional programming style.
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 vs slice with StartIndex
Comments
Confirm delete:
Do you really want to delete benchmark?