perf - regex vs includes vs indexOf (v2)

Revision 2 of this benchmark created on


Setup

const words = ['abc', 'def', 'efg', 'xyz', 'verylongword'];


const text = 'sample text. abc. with lots of text lorem ipsum something something. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. xyz. Maecenas ut placerat ligula. Pellentesque vitae urna eget massa facilisis volutpat. Integer non mi arcu. Cras eu hendrerit metus. Donec aliquet iaculis nibh, et tristique lectus commodo nec. Curabitur scelerisque sapien ut mauris commodo hendrerit vel in orci. Ut dapibus accumsan erat, eget sodales metus. Nullam faucibus turpis eget ipsum congue gravida. Fusce finibus id urna in viverra. Nunc at auctor lectus. Pellentesque nec nunc nec risus mollis eleifend ac in nunc. Praesent eget tempus sapien. Suspendisse rhoncus diam id imperdiet ullamcorper. Aenean eleifend turpis nec ligula euismod tincidunt. verylongword'

Test runner

Ready to run.

Testing in
TestOps/sec
regex
const used = words.filter((word) => new RegExp(word).test(text, 'i'));


ready
includes
const used = words.filter((word) => text.toLowerCase().includes(word.toLowerCase()));


ready
indexOf
const used = words.filter((word) => text.toLowerCase().indexOf(word.toLowerCase()) > -1);


ready
includes - cached
const textLower = text.toLowerCase()
const used = words.filter((word) => textLower.includes(word.toLowerCase()));


ready
indexOf - cached
const textLower = text.toLowerCase()
const used = words.filter((word) => textLower.indexOf(word.toLowerCase()) > -1);


ready

Revisions

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