TextEncoder versus RegEx

Benchmark created on


Description

Not matching the exact same thing, only for perf test reasons

Setup

// Sample strings of length 50 each
const testSize = 100000;
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-!?#'ï[].";
const multiBy = "👩🏽‍🤝‍👨🏼";
const samples = (new Array(testSize)).fill("").map((str, j) => {
    for (let i = 0; i < 50; i++) str += charset.charAt(Math.floor(Math.random() * 72));
    if (j%10 === 0) str += multiBy;
    return str;
});
const te = str => str.length !== (new TextEncoder().encode(str)).length;
const rx = str => !/[^\x00-\x7F]/.test(str);

Test runner

Ready to run.

Testing in
TestOps/sec
TextEncoder
const teRes = samples.map(te);
ready
RegEx
const rxRes = samples.map(rx);
ready

Revisions

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