Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Height calculations: RegEx vs split()
(version: 1)
Calculating height in inches from a string
Comparing performance of:
RegEx vs split()
Created:
4 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
const heights = [`6'`, `5'3"`, `11"`, `0'8"`, `4'0"`]
Tests:
RegEx
const heightRegex = /(?:^|(\d+)['])(?:$|(\d+)["])/ for (const height of heights) { const result = height.match(heightRegex); console.log("Height in inches: ", ((result[1] ?? 0) * 12) + (result[2] ?? 0)); }
split()
for (const height of heights) { const hasFeet = height.includes(`'`); const hasInches = height.includes(`"`); let value = 0 if (hasFeet && hasInches) { const heightArray = height.split(`'`) const feet = Number(heightArray[0]) const inches = Number(heightArray[1]?.split(`"`)[0]) value = feet * 12 + inches; } else if (hasFeet) { value = Number(height.split(`'`)[0]) * 12 } else if (hasInches) { value = Number(height.split(`"`)[0]); } console.log("Height in inches: ", value) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx
split()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 OPR/126.0.0.0 (Edition developer)
Browser/OS:
Opera 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx
28153.5 Ops/sec
split()
25801.2 Ops/sec
Related benchmarks:
Define Regex inline vs not inline...
parse number from string
Split vs Regex Iteration
Split vs Regex vs Replace Iteration
Split vs regex for line numbering
test regex vs loop
regex vs slice
Regex vs Forloop Performance
Array manual search vs Regex.test
Comments
Confirm delete:
Do you really want to delete benchmark?