Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date JsonReviver8
(version: 0)
Comparing performance of:
Baseline vs Regex vs No check vs Length
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var input = []; for (i = 2; i <= 100000; i++) { const date = new Date(Math.random()*1000); const value = i % 2 === 0 ? date.toISOString() : 'some random value'; const key = i % 2 === 0 ? 'businessDate' : 'some random key'; input.push({ key, value }); input.push({ key: 'index', value: i }); input.push({ key: 'updatedAt', value: i }); input.push({ key: 'a', value: Math.random() }); input.push({ key: 'b', value: Math.random() }); input.push({ key: 'c', value: Math.random() }); input.push({ key: 'd', value: Math.random() }); input.push({ key: 'e', value: Math.random() }); }
Tests:
Baseline
const result = input.map(x=>{ if (typeof x.value !== 'string') { return x.value; } switch (x.key) { case 'businessDate': case 'startDate': case 'endDate': case 'createdAt': case 'updatedAt': return new Date(x.value); default: return x.value; } });
Regex
const result = input.map(x=>{ if (typeof x.value !== 'string') { return x.value; } switch (x.key) { case 'businessDate': case 'startDate': case 'endDate': if (/(\d{4})-(\d{2})-(\d{2})/.test(x.value)) { return new Date(x.value + 'T00:00:00.000'); } return new Date(x.value); case 'createdAt': case 'updatedAt': return new Date(x.value); default: return x.value; } });
No check
const result = input.map(x=>{ if (typeof x.value !== 'string') { return x.value; } switch (x.key) { case 'businessDate': case 'startDate': case 'endDate': return new Date(x.value + 'T00:00:00.000'); case 'createdAt': case 'updatedAt': return new Date(x.value); default: return x.value; } });
Length
const result = input.map(x=>{ if (typeof x.value !== 'string') { return x.value; } switch (x.key) { case 'businessDate': case 'startDate': case 'endDate': if (x.value.length === 10) { return new Date(x.value + 'T00:00:00.000'); } return new Date(x.value); case 'createdAt': case 'updatedAt': return new Date(x.value); default: return x.value; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Baseline
Regex
No check
Length
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON benchmark. The test case involves creating an array of objects with various key-value pairs, including dates and numbers. The goal is to measure how quickly different JavaScript engines can iterate over this array and perform operations on its elements. **Options being compared:** 1. **Baseline**: This option checks if the value is a string using `typeof x.value !== 'string'`. It then performs a simple switch statement to determine which date format to use. 2. **Regex**: This option uses a regular expression (`/(\\d{4})-(\\d{2})-(\\d{2})/.test(x.value)`) to check if the value is in the ISO 8601 date format (YYYY-MM-DDTHH:MM:SS.SSSZ). If it matches, it converts the string to a Date object. 3. **No check**: This option skips the `typeof` check and directly uses the `switch` statement to convert dates to Date objects based on their keys. 4. **Length**: This option checks if the length of the value is exactly 10 characters (e.g., for ISO 8601 dates). If it matches, it converts the string to a Date object. **Pros and cons of each approach:** * **Baseline**: Pros: simple and efficient; Cons: may not handle all date formats correctly. If the engine can't determine the format from the key, it will default to the `default` branch, which might be slower. * **Regex**: Pros: robust and flexible; Cons: regular expressions can be computationally expensive and may introduce unnecessary overhead. The engine must also compile the regex pattern, which can add latency. * **No check**: Pros: simple and fast; Cons: assumes that all dates are in a specific format (based on the key), which might not always be the case. * **Length**: Pros: efficient for ISO 8601 dates; Cons: may not handle other date formats correctly. **Browser-specific results:** The latest benchmark result shows performance variations across different browsers and environments: * Chrome 99 performs well in all options ( Baseline, Regex, No check, Length). * The `Length` option outperforms the others by a small margin. * The `Baseline` option comes closest to the other options. **Conclusion:** The benchmark highlights the importance of correctly handling date formats and regular expressions in JavaScript engines. While each approach has its pros and cons, the best-performing option is **Length**, which leverages the efficient compilation and execution of regex patterns for ISO 8601 dates. However, it's essential to consider other factors like code maintainability, flexibility, and potential errors when choosing a solution.
Related benchmarks:
Date JsonReviver2
Date JsonReviver4
Date JsonReviver5
Date JsonReviver7
Comments
Confirm delete:
Do you really want to delete benchmark?