Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter unsafe token
(version: 0)
Comparing performance of:
Deep Clone vs Iterate All keys vs Deep Clone, but not parse
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var params = Array.from({length: 30}).reduce((p, n, i) => { p['key' + i] = 'value' + i; return p; }, {}); function runTest(fn, cnt) { while(cnt--) {fn();} }
Tests:
Deep Clone
function fixParams(params) { const token = 'xxxxxxxxxxxxxxxxxxxx'; if (typeof params === 'object' && token) { try { const reg = new RegExp(token, 'g'); const str = JSON.stringify(params); str.replace(reg, 'yyyyyyyyyyyyy'); params = JSON.parse(str); } catch (e) { console.log(e); } } return params; } runTest(() => fixParams(params), 10000)
Iterate All keys
function deepReplace(params, regex, replacement) { for (let key in params) { if (typeof params[key] === 'object') { deepReplace(params[key], regex, replacement); } else if (regex.test(params[key])) { params[key] = params[key].replace(regex, replacement); } } } function fixParams(params) { const token = 'xxxxxxxxxxxxxxxxxxxx'; if (typeof params === 'object' && token) { try { const reg = new RegExp(token, 'g'); params = deepReplace(params, reg, 'yyyyyyyyyyyyy'); } catch (e) { console.log(e); } } return params; } runTest(() => fixParams(params), 10000)
Deep Clone, but not parse
function fixParams(params) { const token = 'xxxxxxxxxxxxxxxxxxxx'; if (typeof params === 'object' && token) { try { const reg = new RegExp(token, 'g'); const str = JSON.stringify(params); if (reg.test(str)) { str.replace(reg, 'yyyyyyyyyyyyy'); params = JSON.parse(str); } } catch (e) { console.log(e); } } return params; } runTest(() => fixParams(params), 10000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Deep Clone
Iterate All keys
Deep Clone, but not parse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Deep Clone
43.6 Ops/sec
Iterate All keys
89.4 Ops/sec
Deep Clone, but not parse
74.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark is designed to test the performance of JavaScript engines in replacing or cloning objects with specific properties, specifically using regular expressions. **Benchmark Definition** The benchmark definition is represented by a JSON object that contains the script preparation code and HTML preparation code (which is empty in this case). The script preparation code creates an array of objects `params` with unique keys and values. A function `runTest` is defined to repeatedly execute another function, which takes a variable number of arguments. The benchmark definition itself is represented by two JSON objects: * "Deep Clone": This test case checks if the JavaScript engine can properly clone an object without modifying its properties. * "Iterate All keys" and "Deep Clone, but not parse": These tests check if the engine can iterate over all keys in an object and replace values only for specific regular expression patterns. **Options Compared** The benchmark compares three different approaches: 1. **Deep Clone**: This approach replaces or clones objects with specific properties without modifying their structure. 2. **Iterate All Keys**: This approach iterates over all keys in an object, but only performs replacement operations for a specific regular expression pattern. 3. **Deep Clone, but not parse**: This approach is similar to the "Deep Clone" test case but does not perform JSON parsing. **Pros and Cons of Each Approach** * **Deep Clone**: * Pros: Provides an accurate measure of object cloning performance without modifying the original structure. * Cons: May be slower due to the need for deep copying or string manipulation. * **Iterate All Keys**: * Pros: May be faster since it only iterates over specific keys and performs replacements, reducing computational overhead. * Cons: Does not provide an accurate measure of object cloning performance, as it may skip over certain keys or values. * **Deep Clone, but not parse**: * Pros: Combines the benefits of "Deep Clone" with reduced JSON parsing overhead, potentially providing better performance compared to the other two approaches. * Cons: May still be slower than iterative approach due to the need for string manipulation. **Library Used** The provided benchmark uses several libraries or functions: * `Array.from()`: Creates an array from an iterable object. * `JSON.stringify()` and `JSON.parse()`: Convert JavaScript objects to strings and vice versa. * `RegExp`: A regular expression engine used for pattern matching and replacement. **Special JS Feature** The benchmark uses the `var` keyword for variable declaration, which is a legacy syntax in JavaScript. It does not use modern ES6+ features like `let`, `const`, or arrow functions, as those were not present at the time of the benchmark creation. **Alternatives** Other alternatives to measure object cloning performance could include: * Using other regular expression engines like PCRE (Perl-Compatible Regular Expressions). * Comparing with other languages' string manipulation and object cloning libraries. * Utilizing hardware-accelerated JavaScript engines or WebAssembly for better performance benchmarks. Keep in mind that the choice of approach depends on the specific requirements and characteristics of the benchmark.
Related benchmarks:
Lodash vs Native reduce on object
Lodash vs Native reduce on object - updated 2
Lodash vs Native reduce on object x100
JS Filter vs Reduce vs Foreach
reduce vs filter 22
Comments
Confirm delete:
Do you really want to delete benchmark?