Word count (v2)

Revision 2 of this benchmark created on


Setup

const text = new Array(10000).fill("word").join(" ")

Test runner

Ready to run.

Testing in
TestOps/sec
Split
const regexp = /\s+/;
const count = text.split(regexp).length
ready
Regexp Iterator
const regexp = /\w+/g;
let count = 0;
text.matchAll(regexp).forEach(() => count++ )
ready
Regexp Match
const regexp = /[^\s\u200b]+/g;
const match = text.match(regexp)
const count = match ? match.length : 0
ready
Sticky Regexp
const regexp = /\s*[^\s\u200b]+/y;
let count = 0;
while (regexp.exec(text) !== null){ count ++ }
ready
Words
const regexp = /\s*\w+/y;
let count = 0;
while (regexp.exec(text) !== null){ count ++ }
ready
Split plain
const str = "";
const count = text.split(str).length 

ready

Revisions

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