Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
misc tests
(version: 0)
Comparing performance of:
slice vs substr vs substring
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
slice
const date = '1234' date.slice(-2)
substr
const date = '1234' date.substr(2)
substring
const date = '1234' date.substring(2, 4)
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 microbenchmarks on MeasureThat.net. **What is tested?** The provided JSON represents three individual test cases that measure the performance of string manipulation functions in JavaScript: `slice`, `substr`, and `substring`. Each test case consists of a simple script that creates a date string, extracts a subset of characters from it using one of these functions, and then measures how many times each function is executed per second. **Options compared** The three options being tested are: 1. **`slice()`**: Returns a new string containing the last `n` characters of an existing string. 2. **`substr()`**: Returns a substring from the beginning of an existing string, with optional start and end indices. 3. **`substring()`** (in modern JavaScript): Returns a substring from the beginning of an existing string, with optional start and end indices. Each function has its own strengths and weaknesses: * `slice()`: Simple and efficient, but limited to extracting the last `n` characters. * `substr()`: More flexible than `slice`, allowing extraction of arbitrary substrings. * `substring()` (in modern JavaScript): Similar to `substr`, but with optional start and end indices for more flexibility. **Pros and Cons** Here's a brief summary: * **`slice()`**: Pros: simple, efficient. Cons: limited to extracting the last `n` characters, may not be optimal for large strings. * **`substr()`**: Pros: flexible, suitable for most string extraction tasks. Cons: slightly slower than `slice`, as it involves more operations. * **`substring()`** (in modern JavaScript): Pros: similar flexibility to `substr`, with optional start and end indices. Cons: may be slower due to additional checks. **Library usage** None of the test cases use any external libraries, so there's no library-specific optimization or overhead to consider. **Special JS feature or syntax** The only special feature mentioned is the use of `Modern JavaScript` in one of the test cases (`substring`). This implies that the benchmark might be sensitive to the version of JavaScript being used. However, without more context, it's difficult to determine the exact implications of this feature. **Other alternatives** If you're interested in exploring alternative string manipulation functions or optimizations, some options might include: * **`indexOf()`**, `lastIndexOf()`, and related methods for searching substrings. * **Regular expressions (regex)** for pattern matching and extraction. * **Array.prototype.slice()`, Array.prototype.substr(), and similar methods for working with arrays of strings. Keep in mind that these alternatives may have different performance characteristics, trade-offs, or use cases compared to the original `slice()`, `substr()`, and `substring()` functions.
Related benchmarks:
Optional Chaining versus _.get lodash 1
Optional Chaining versus _.get lodash versus ?.
Optional Chaining versus _.get lodash vs ES11 Optional Chaining
Optional Chaining versus _.get lodash and with array
reduce vs flatmap recursive
Comments
Confirm delete:
Do you really want to delete benchmark?