const str = "https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/"
str.includes("firebasestorage")
const str = "https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/"
str.startsWith("https://fire")
const str = "https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/"
str.indexOf("https://fire") === 0
const str = "https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/"
str.indexOf("firebasestorage") > -1
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
startsWith | |
indexOf starts | |
indexOf somewhere |
Test name | Executions per second |
---|---|
includes | 39012628.0 Ops/sec |
startsWith | 26160034.0 Ops/sec |
indexOf starts | 918712256.0 Ops/sec |
indexOf somewhere | 921924928.0 Ops/sec |
I'll break down the benchmark test cases and explain what's being tested, compared, and their pros and cons.
Benchmark Definition Json
The provided JSON defines a benchmark with no script preparation code or HTML preparation code. This means that the benchmarks are designed to be run directly in the browser without any additional setup.
Test Cases
There are four individual test cases:
includes
startsWith
indexOf starts
indexOf somewhere
Let's analyze each test case:
includes
"Benchmark Definition": "const str = \"https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/\"\r\nstr.includes(\"firebasestorage\")"
This benchmark tests the performance of the includes
method, which returns true
if a string contains a specified value.
Pros and Cons:
startsWith
or indexOf
when searching for a specific substring.startsWith
"Benchmark Definition": "const str = \"https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/\"\r\nstr.startsWith(\"https://fire\")"
This benchmark tests the performance of the startsWith
method, which returns true
if a string starts with a specified value.
Pros and Cons:
includes
since it only requires checking the first few characters.indexOf starts
"Benchmark Definition": "const str = \"https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/\"\r\nstr.indexOf(\"https://fire\") === 0"
This benchmark tests the performance of the indexOf
method with a strict equality check, which returns 0
if the string starts with a specified value.
Pros and Cons:
includes
and startsWith
.startsWith
alone due to the additional equality check.indexOf somewhere
"Benchmark Definition": "const str = \"https://firebasestorage.googleapis.com/v0/b/a-sketch-a-day-207420.appspot.com/o/\"\r\nstr.indexOf(\"firebasestorage\") > -1"
This benchmark tests the performance of the indexOf
method with a non-strict equality check, which returns true
if the string contains a specified value anywhere.
Pros and Cons:
includes
or startsWith
for certain inputs.startsWith
for prefixes.Library: None
There are no libraries used in these benchmarks. The methods being tested are built-in JavaScript string methods.
Special JS Features or Syntax: None
There are no special JavaScript features or syntax mentioned in the benchmark definitions.
Other Alternatives
Other alternatives to these benchmarked methods include:
includes
or startsWith
Keep in mind that the performance differences between these methods may vary depending on the specific use case and input data.