Word count

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

console.log("Split", count)
ready
Regexp Iterator
const regexp = /\w+/g;
let count = 0;
text.matchAll(regexp).forEach(() => count++ )

console.log("Iterator", count)
ready
Regexp Match
const regexp = /[^\s\u200b]+/g;
const match = text.match(regexp)
const count = match ? match.length : 0

console.log("Match", count)
ready
Sticky Regexp
const regexp = /\s*[^\s\u200b]+/y;
let count = 0;
while (regexp.exec(text) !== null){ count ++ }

console.log("Sticky", count)
ready
Words
const regexp = /\s*\w+/y;
let count = 0;
while (regexp.exec(text) !== null){ count ++ }

console.log("StickyWords", count)
ready

Revisions

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