Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substring vs split 2
(version: 0)
Comparing performance of:
split vs substr vs clone with split
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; var s2 = "b"; var s3 = "asfdasfasdfdsafkjahsdfjkhsdajkfhasjfhd";
Tests:
split
let temp; for (let i = 0;i < s1.length; i++) { temp = s1.split('').splice(i, 1); } for (let i = 0;i < s2.length; i++) { temp = s2.split('').splice(i, 1); } for (let i = 0;i < s3.length; i++) { temp = s3.split('').splice(i, 1); }
substr
let temp; for (let i = 0;i < s1.length; i++) { temp = s1.substring(0, i) + s1.substring(i + 1); } for (let i = 0;i < s2.length; i++) { temp = s2.substring(0, i) + s2.substring(i + 1); } for (let i = 0;i < s3.length; i++) { temp = s3.substring(0, i) + s3.substring(i + 1); }
clone with split
let temp; let stringArray = s1.split('') for (let i = 0;i < s1.length; i++) { temp = [...stringArray].splice(i, 1); } stringArray = s2.split('') for (let i = 0;i < s2.length; i++) { temp = [...stringArray].splice(i, 1); } stringArray = s3.split('') for (let i = 0;i < s3.length; i++) { temp = [...stringArray].splice(i, 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
split
substr
clone with split
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. **What is being tested?** The benchmark measures the performance of three different approaches to extract substrings from strings: 1. **`split`**: This method splits a string into an array of substrings using a specified separator (in this case, an empty string `''`, which means it's splitting at each character). 2. **`substring`**: This method returns a new substring that starts at the specified start index and has the specified length. 3. **`clone with split`**: This approach first splits the original string into an array of substrings using `split('')`, then clones this array, splices out the desired substring, and repeats for each input string. **Options compared** The three approaches are compared in terms of their performance in extracting substrings from strings. The benchmark measures the number of executions per second (ExecutionsPerSecond) on a desktop browser with Chrome 102. **Pros and Cons:** 1. **`split`**: Pros: * Simple and efficient. * Can be used for multiple purposes, such as splitting at specific separators or creating arrays of substrings. Cons: * May not be suitable for all use cases, especially when dealing with large strings or complex separators. 2. **`substring`**: Pros: * Efficient for extracting a single substring from a string. * Easy to understand and implement. Cons: * Can be slow for larger strings or multiple extractions. 3. **`clone with split`**: Pros: * Can handle multiple extractions efficiently by cloning the array first. Cons: * Requires creating an additional array, which can lead to memory overhead. * May have slower performance due to cloning and splicing operations. **Library usage** None of the approaches use any external libraries. They are built-in JavaScript methods or simple variations using the `split` method. **Special JS features** The benchmark uses some special JavaScript features: 1. **Template literals**: Used in the script preparation code to define strings with escaped newlines (`\r\n`) and quotes. 2. **Arrow functions**: Not used explicitly, but the benchmark scripts are concise and use arrow function-like syntax. 3. **Array destructuring**: Used in the `clone with split` approach to create a new array from the result of `split('')`. **Other alternatives** If you're interested in exploring alternative approaches, here are some suggestions: 1. **Using regex**: You can use regular expressions to extract substrings from strings using methods like `String.prototype.match()`. 2. **Custom implementation**: If performance is critical, you might consider implementing a custom substring extraction function using bitwise operations or other low-level techniques. 3. **Native array methods**: Modern JavaScript engines provide native array methods for string manipulation, such as `String.prototype.slice()` or `Array.prototype.subarray()`. You can explore these alternatives to see if they offer better performance. Keep in mind that the benchmark result is specific to Chrome 102 on a desktop browser with Windows, so results may vary when running the benchmark on different environments.
Related benchmarks:
Array.split vs string.substring
Array split vs string substring ISO String
Array split vs string substring22
Array split vs string substring substring
Comments
Confirm delete:
Do you really want to delete benchmark?