Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs substr vs substring - long text2
(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:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var example = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).'
Tests:
slice
var result = example.slice(25)
substr
var result = example.substr(25)
substring
var result = example.substring(25)
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:
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):
Let's dive into the world of JavaScript benchmarks! **Benchmark Overview** The provided JSON represents a benchmark that compares the performance of three string slicing methods: `slice()`, `substr()`, and `substring()`. **Test Case Breakdown** Each test case is defined by a separate object in the "Individual test cases" array. Here's what each test case tests: * **slice**: Tests if `slice()` can be used to extract a substring starting from index 25. * **substr**: Tests if `substr()` can be used to extract a substring starting from index 25 (note that `substr()` is typically used with two arguments: start and length). * **substring**: Tests if `substring()` can be used to extract a substring starting from index 25. **Library** In this benchmark, none of the test cases use any external libraries. The benchmark relies on built-in JavaScript methods. **Special JS Features or Syntax** The only special feature used in this benchmark is the fact that some browsers (specifically Chrome) allow using `slice()` with a single argument to start from index 0. This behavior is not part of the standard ECMAScript specification, but it's supported by some popular browsers. **Options Compared** Here are the options compared: * **Slice**: Extracts the substring starting from index 25. * **Substr**: Not suitable for this test case because it typically requires two arguments (start and length). * **Substring**: Extracts the substring starting from index 25, which is not supported by older browsers like Internet Explorer. **Pros and Cons** Here are some pros and cons of each approach: * **Slice**: Pros: + Fast execution time + Widely supported by modern browsers Cons: + Not suitable for all use cases (e.g., requiring two arguments) * **Substr**: Pros: None Cons: + Not suitable for this test case due to its typical usage with two arguments. * **Substring**: Pros: None Cons: + Not widely supported by older browsers. **Other Alternatives** If you were to rewrite this benchmark, you could consider using alternative methods like: * Using `indexOf()` and concatenation to extract the substring. * Using regular expressions (regex) to extract the substring. * Using a library like Lodash or String.prototype.slice() with a workaround for older browsers. However, keep in mind that these alternatives might not provide accurate results due to differences in browser support and performance.
Related benchmarks:
TreeWalker vs querySelectorAll (* all elements)
NodeIterator - filter function vs nextNode state check
TreeWalker vs querySelectorAll *
Traverse function vs NodeIterator vs TreeWalker for TEXT nodes
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?