replaceAllvsMap

Benchmark created on


Description

.replaceAll() vs .map()

let's find out

Setup

const generateTestString = (length, whitelist = []) => {
  const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789, ";
  return Array.from({ length }, () => chars[Math.floor(Math.random() * chars.length)]).join('');
};

const testString = generateTestString(100000, ["o", ",", " "]);
const maskChar = "*";
const whiteList = ["o", ",", " "];

Test runner

Ready to run.

Testing in
TestOps/sec
replaceAll
testString.replaceAll(new RegExp(`[^${whiteList.join('')}]`, "g"), maskChar);
ready
map
testString.split("").map(char => whiteList.includes(char) ? char : maskChar).join("");
ready

Revisions

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