regex with 4 capture groups vs. 4 slices

Benchmark created on


Setup

let hexStr = "aabbccdd";

Test runner

Ready to run.

Testing in
TestOps/sec
regex
        const match = hexStr.match(/([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?/);

        // slice(1) removes the full match, map prefixes remaining captures
        const vals = match.slice(1).filter(Boolean).map(p => "0x" + p);
        
        return true;
ready
slices
const vals = [];

vals[0] = hexStr.slice(0,2);
vals[1] = hexStr.slice(2,4);
vals[2] = hexStr.slice(4,6);
vals[3] = hexStr.slice(6,8);
ready

Revisions

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