string .startsWith() vs index[] vs .slice()

Benchmark created on


Setup

const str = (Math.random() + 1).toString(36).substring(7);
let res = false;

Teardown

console.log(res);

Test runner

Ready to run.

Testing in
TestOps/sec
.startsWith()
res = str.startsWith('a');
ready
index[]
res = str[0] === 'a';
ready
.slice()
res = str.slice(0, 1) === 'a';
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.