var example = 'there is no spoon'
var result = example.slice(0,10)
var result = example.substr(0,10)
var result = example.substring(0,10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
substr | |
substring |
Test name | Executions per second |
---|---|
slice | 166101792.0 Ops/sec |
substr | 171870928.0 Ops/sec |
substring | 92257944.0 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and the pros and cons of each approach.
What is being tested?
The benchmark is designed to compare three different string manipulation methods in JavaScript: slice()
, substr()
, and substring()
. The test case uses a fixed string example = 'there is no spoon'
.
Options compared
var result = example.slice(0,10)
example
from index 0 up to (but not including) index 10.var result = example.substr(0,10)
example
starting at index 0 up to (but not including) index 10.var result = example.substring(0,10)
example
from index 0 up to (but not including) index 10.Other considerations
The benchmark provides results for the three methods:
Test Name | Browser | DevicePlatform | OperatingSystem | ExecutionsPerSecond |
---|---|---|---|---|
slice | Chrome 108 | Desktop | Linux | 5242033.0 |
substr | Chrome 108 | Desktop | Linux | 5233290.0 |
substring | Chrome 108 | Desktop | Linux | 4146559.75 |
Pros and Cons of Each Approach:
Alternatives
indexOf()
, lastIndexOf()
, or regular expressions (RegExp
) are not being tested.