Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Regex v2
(version: 0)
Comparing performance of:
Reduce vs Regex
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Reduce
const excludedFields = ['page', 'sort', 'limit', 'fields'] const input = { price: { gte: 500 }, page: 5 } const transform = obj => Object.entries(obj).reduce( (acc, [val, key]) => ({ ...acc, [`$${val}`]: key }), {} ) const output = Object.entries(input).reduce( (acc, [key, val]) => excludedFields.includes(key) ? { ...acc } : { ...acc, [key]: val instanceof Object ? transform(val) : val }, {} ) console.log(output)
Regex
const excludedFields = ['page', 'sort', 'limit', 'fields'] const input = { price: { gte: 500 }, page: 5 }; const queryObj = { ...input } excludedFields.forEach(el => delete queryObj[el]) let filterQuery = JSON.stringify(queryObj) filterQuery = filterQuery.replace( /\b(gte|gt|lte|lt)\b/g, match => `$${match}` ) const output = JSON.parse(filterQuery) console.log(output)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Regex
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):
I'll break down the provided JSON and benchmark test cases to explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The provided JSON represents a benchmark definition for MeasureThat.net, a website that allows users to create and run JavaScript microbenchmarks. The `Name` field is "Reduce vs Regex v2", indicating the two approaches being compared: using `reduce()` in JavaScript and using regular expressions (`regex`) to filter data. **Test Cases** There are only two test cases: 1. **"Reduce"`** * This approach uses the `reduce()` method to iterate over an object's entries, excluding certain fields. * The script preparation code defines a function `transform` that takes an object as input and returns a new object with keys prefixed using `$`. * The main code then uses another `reduce()` call to filter out excluded fields from the original object and applies the transformation if the value is an object. 2. **"Regex"`** * This approach uses regular expressions to filter out excluded fields from the input data. * The script preparation code defines a string `filterQuery` that includes placeholders for excluded fields, which are then replaced with actual values using regex replacement. * The main code parses the resulting JSON string to obtain the filtered output. **Comparison and Pros/Cons** The two approaches differ in performance and complexity: * **"Reduce"`**: This approach is likely faster and more efficient because it uses a built-in `reduce()` method, which is optimized for performance. However, it may be less flexible or reusable, as it's tightly coupled to the specific transformation logic. + Pros: Efficient, flexible (with additional transformation logic). + Cons: May require additional setup or library dependencies for more complex transformations. * **"Regex"`**: This approach uses regular expressions, which can be slower and less efficient due to the overhead of regex parsing and matching. However, it provides more flexibility and reusability, as the same regex pattern can be applied to different data formats. + Pros: Flexible, reusable (with custom regex patterns). + Cons: May be slower and less efficient than `reduce()`. **Library Usage** In this benchmark, no specific JavaScript libraries are used beyond the built-in `JSON` functions. However, if additional libraries were required for more complex transformations or data manipulation, options might include: * Lodash (e.g., for array methods like `filter()`, `map()`) * Underscore.js (e.g., for functional programming utilities) * Other custom libraries depending on the specific requirements. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark beyond the standard ECMAScript 5+ language.
Related benchmarks:
str split vs regex replace
Regex vs Split Time
String.match vs. RegEx.test vs trim
str.match vs str.Split3322
JavaScript string split vs match using regex
Comments
Confirm delete:
Do you really want to delete benchmark?