Regex: Word Boundary vs No Boundary (Russian Text)

Benchmark created on


Description

For shorter words, no \b is better than some: https://jsperf.app/kelizi , or so I guess. So now here's a real word "неземной" + a real text.

Compare \b[Нн]еземной\b (strict whole-word match) vs [Нн]еземной (substring match) in a dictionary entry.

Setup

// Preparation JS
const text = `
Значение слова «неземной»
НЕЗЕМНО́Й, -а́я, -о́е.
1. Находящийся вне Земли... неземные тела...
2. Трад.-поэт. Потусторонний... неземной...
|| перен. Чуждый всего... неземного...
Источник: Словарь русского языка...
НЕЗЕМНО'Й, а́я, о́е... некто неземной...
`;

Test runner

Ready to run.

Testing in
TestOps/sec
With word boundaries
// Test 1: With word boundaries (\b)
const testWithBoundary = () => {
  const regex = /\b[Нн]еземной\b/g;
  return text.match(regex)?.length || 0;
};
ready
Without word boundaries
// Test 2: Without word boundaries
const testWithoutBoundary = () => {
  const regex = /[Нн]еземной/g;
  return text.match(regex)?.length || 0;
};
ready

Revisions

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