Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
parse date
(version: 0)
Comparing performance of:
Regex vs Substr
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var REGEX = /^(\d{4})-(\d{2})-(\d{2})(T(\d{2}):(\d{2})(:(\d{2}))?)?/; function withRegex(str) { var match = str.match(REGEX); if (match) { return new Date(match[1], match[2], match[3], match[5] || 0, match[6] || 0, match[8] || 0); } return null; } function withSubstr(str) { var year = parseInt(str.substr(0, 4), 10), month = parseInt(str.substr(5, 2), 10) - 1, day = parseInt(str.substr(8, 2), 10), hour = 0, minute = 0, second = 0; if (str.indexOf('T') > -1) { hour = parseInt(str.substr(11, 2), 10), minute = parseInt(str.substr(14, 2), 10), second = parseInt(str.substr(17, 2), 10); } return new Date(year, month, day, hour, minute, second); }
Tests:
Regex
for (var i=0; i<100; ++i) { withRegex('1991-11-26T12:00:00'); withRegex('1991-11-26'); }
Substr
for (var i=0; i<100; ++i) { withSubstr('1991-11-26T12:00:00'); withSubstr('1991-11-26'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
Substr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 135 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
11373.5 Ops/sec
Substr
13741.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose** The benchmark is designed to compare two approaches for parsing dates in JavaScript: using a regular expression (Regex) and using the `substr` method. **Options Compared** Two options are compared: 1. **withRegex**: This approach uses a custom regular expression to parse the date string. 2. **withSubstr**: This approach uses the `substr` method to extract individual components of the date string (year, month, day, hour, minute, and second). **Pros and Cons** **withRegex** Pros: * More concise and expressive syntax for parsing dates * Can handle more complex date formats Cons: * May be slower due to the overhead of regular expression compilation and matching * Requires defining a custom regex pattern, which can be error-prone if not done correctly **withSubstr** Pros: * Faster execution speed due to the simplicity of the `substr` method * Does not require defining a custom regex pattern or compiling it Cons: * Less concise and less expressive syntax for parsing dates * May not handle all possible date formats correctly, especially those with non-standard separators or encoding **Other Considerations** The benchmark also considers the number of executions per second (ExecutionsPerSecond) in each browser, which indicates performance. **Library Used** In the `withRegex` function, a custom regex pattern is used. The library being referenced here is not explicitly mentioned, but it's likely that the JavaScript engine or runtime environment uses some kind of regex library under the hood. **Special JS Feature/Syntax** There are no special JS features or syntaxes being tested in this benchmark. Both approaches use standard JavaScript features. **Alternatives** If you were to rewrite this benchmark, you could consider other approaches for parsing dates, such as: 1. Using a dedicated date parsing library like Moment.js. 2. Utilizing built-in JavaScript functions like `Date.parse()` or `new Date()`. 3. Implementing a custom parser that combines elements of both the Regex and Substr approaches. Keep in mind that each approach has its trade-offs, and the best choice depends on the specific requirements and constraints of your project.
Related benchmarks:
parse date
parse date
parse date
split1 vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?