Regex testing whitespace vs trim
Date tested:
one year ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Test name
Executions per second
regex
6483915.5 Ops/sec
trim
12761210.0 Ops/sec
regex new init every time
9557902.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var string = ' '; var regPattern = new RegExp(/\A\s*\z/g);
Tests:
regex
regPattern.test(string)
trim
string.trim().length === 0
regex new init every time
/^\s*$/.test(string)
Open this result on MeasureThat.net