Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Performance Test: substring vs slice vs split
(version: 0)
Comparing performance of:
slice vs substring vs split
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII="
Tests:
slice
var substring = string.slice(string.indexOf(";base64,")+1);
substring
var substring = string.substring(string.indexOf(";base64,")+1);
split
var substring = string.split(/data:|;base64,/)[2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice
substring
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different approaches to extracting a substring from a provided string: * **`slice()`:** This method takes two arguments: a starting index and an optional ending index. It returns a new string containing the characters from the start index up to (but not including) the end index. * **`substring()`:** This method also takes two arguments, a starting index and an optional ending index. Similar to `slice()`, it returns a new string containing the characters from the start index to the end index. * **`split()`:** This method takes a regular expression as its argument and returns an array of strings. In this benchmark, it's used to split the string by ";base64," and then select the third element of the resulting array. **Pros/Cons:** * **`slice()` and `substring()` are generally faster because they directly access and extract a portion of the original string.** They avoid creating a new array like `split()` does. * **`split()` can be more versatile if you need to split the string based on multiple patterns or criteria beyond just extracting a substring.** However, it comes with the overhead of creating an array, which can impact performance. **Alternatives:** * You could use regular expressions (`RegExp`) for extraction, but that might add complexity and potentially slow down the process compared to `slice()` or `substring()`. **Considerations:** * **String Length:** The length of the input string will significantly affect performance. * **Frequency of Use:** If this substring extraction is done frequently within your code, then optimizing for speed with `slice()` or `substring()` might be worthwhile. Let me know if you have any other questions about JavaScript benchmarking or specific syntax!
Related benchmarks:
copyWithin vs iterate (with GPU Raster and A Larger Image)
Performance Test: substring vs slice vs split 2
Performance of replace() vs slice() vs split
Array.push() vs map() for converting a string into a char array
Comments
Confirm delete:
Do you really want to delete benchmark?