Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs substr vs slice, long string
(version: 0)
Comparing performance of:
slice vs substring vs substr
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eu nibh eget dui lacinia dapibus. In eu neque id sapien tempus egestas et nec metus. Ut pulvinar dolor eros, non sollicitudin enim faucibus ac. Pellentesque ut ex id ante porta volutpat nec sit amet arcu. Donec ut est semper, finibus risus sit amet, facilisis risus. Sed eleifend accumsan metus in laoreet. Etiam nec nisl sed mi euismod accumsan eget posuere urna. Sed mollis convallis efficitur. Duis leo orci, imperdiet eu magna in, efficitur pharetra massa. Morbi ligula felis, iaculis sodales neque in, tempus porttitor urna. In hac habitasse platea dictumst. Etiam lobortis dapibus purus in viverra. Etiam nec dignissim lorem. Aenean tristique euismod hendrerit. Fusce semper finibus porttitor. Pellentesque volutpat ligula eu massa vulputate interdum. Morbi eu scelerisque diam. Pellentesque ornare blandit sem cursus tempor. Vivamus tincidunt porta mi, vitae efficitur mi condimentum quis. Sed varius hendrerit urna a facilisis. In eu nunc sit amet elit tempor fringilla et ut massa. Donec non diam ullamcorper, ultricies sem quis, imperdiet massa. Fusce purus neque, dictum id sodales in, malesuada quis justo. Integer eget iaculis dui, eget feugiat libero. Aliquam erat volutpat. In fermentum luctus gravida. Ut ac cursus libero. Ut tellus eros, imperdiet nec laoreet sit amet, accumsan sed erat. Morbi nec luctus mi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse nulla mi, tempus nec risus et, iaculis ultrices arcu. Curabitur sit amet augue lectus. Ut nec cursus est. Aenean ullamcorper, mauris sed tincidunt ultrices, ante risus elementum nunc, porttitor bibendum enim est ac arcu. Vestibulum varius molestie felis vehicula sollicitudin. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ut maximus turpis. Pellentesque eget luctus tellus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Integer finibus arcu scelerisque sapien viverra fusce."
Tests:
slice
var substring = string.slice(17, 1005);
substring
var substring = string.substring(17, 1005);
substr
var substring = string.substr(17, 988);
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:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice
195571856.0 Ops/sec
substring
213885568.0 Ops/sec
substr
196434144.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark definition, test cases, and latest results in a way that's easy to understand for software engineers of all skill levels. **Benchmark Definition** The benchmark is designed to compare the performance of three string manipulation methods: `slice()`, `substring()`, and `substr()`. The goal is to see which method is faster when extracting a substring from a long string. **Test Cases** There are three test cases: 1. **`slice()`**: Uses the `slice()` method to extract a substring starting at index 17 and ending at index 1005. 2. **`substring()`**: Uses the `substring()` method to extract a substring starting at index 17 and ending at index 1005. 3. **`substr()`**: Uses the `substr()` method to extract a substring starting at index 17 and ending at index 988. **Pros and Cons of Each Approach** * **`slice()`**: This method is relatively modern and is often preferred for its flexibility and readability. However, it may not be supported in older browsers or environments. * **`substring()`**: This method has been around longer than `slice()` but can be less readable due to its more verbose syntax. * **`substr()`**: This method is often used in older codebases or environments where the modern string methods are not available. **Library and Purpose** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that all three methods rely on the browser's built-in string manipulation functions. **Special JS Features or Syntax** * **`slice()`**: This method uses a newer JavaScript feature called "resting" (introduced in ECMAScript 2015) to specify a range of indices. * **`substring()`**: This method uses an older syntax for extracting substrings, which may be less readable than the `slice()` approach. **Latest Results** The latest results show that: * `substr()` is currently the fastest method across all browsers and devices (with average execution times of 2691968.5 executions per second). * `slice()` is slightly slower than `substr()` but still faster than `substring()` (average execution time of 2923347.5 executions per second). * `substring()` is the slowest method, with average execution times ranging from 26362462.0 to 2923347.5 executions per second. **Other Alternatives** If you're interested in exploring alternative methods for string manipulation, some other options include: * Using regular expressions (e.g., `RegExp.prototype.exec()`) to extract substrings or match patterns. * Utilizing libraries like Lodash or Underscore.js, which provide utility functions for working with strings and arrays. However, the benchmark definition specifically compares three native JavaScript methods (`slice()`, `substring()`, and `substr()`), so it's worth sticking with those if performance is a concern.
Related benchmarks:
slice vs substr vs substring (with end index & large string)
str split vs spread (LONG STRINGS) v1
regex vs includes speed comparison
Spilt() vs Substring()
Comments
Confirm delete:
Do you really want to delete benchmark?