JavaScript endsWith() Alternative Method Performance

Benchmark created on


Setup

const greet = 'Hello, JavaScript!';
let pattern = "JavaScript!";

Test runner

Ready to run.

Testing in
TestOps/sec
JavaScript endsWith() method
console.log(greet.endsWith(pattern));  
ready
Regular Expression test() method
let patternText = /JavaScript!$/;
const endsWithPattern = patternText .test(greet);
console.log(endsWithPattern); 
ready
Using substring() alternative method
const endsWithSubstring = greet.substring(greet.lastIndexOf(pattern)) === pattern;
console.log(endsWithSubstring);
// Output: true
ready
Using slice() alternative method
const endsWithSlice = greet.slice(-pattern.length) === pattern;
console.log(endsWithSlice);
ready

Revisions

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