Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cleanJson + delete lock [old vs new]
(version: 4)
Comparing performance of:
cleanJson (old) vs cleanJson (new, ignore lock) vs cleanJson (new, delete lock) vs cleanJson (new, mark lock)
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { "product": "Live JSON generator", "lock": "test", "version": 3.1, "releaseDate": "2014-06-25T00:00:00.000Z", "demo": true, "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": "877-123-1234" }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": "1980-01-02T00:00:00.000Z", "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": "888-555-1212", "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": "parent" } ] } }
Tests:
cleanJson (old)
const cleanJson = (obj) => { // LOOP THROUGH ARRAYS let cleaned; if (obj instanceof Array) { cleaned = []; for (const elem of obj) { cleaned.push(cleanJson(elem)); } return cleaned; // CLEAN AN OBJECT } else if (obj && typeof obj === "object") { cleaned = {}; for (const key of Object.keys(obj)) { // If key starts with "$" it is a private variable // If the key starts with _ we are expecting it to be // _id, __v or __t, all of which we don't allow in the clean // JSON as it is not something we want affecting Mongo switch (key.charAt(0)) { case "$": case "_": continue; } // If the key is not one of those on our persistance // blacklist, we clean the sub-JSON cleaned[key] = cleanJson(obj[key]); } return cleaned; } else { return obj; } }; let newObj = cleanJson(obj); delete newObj.lock;
cleanJson (new, ignore lock)
const cleanJson = (obj) => { let out = obj; if (Array.isArray(obj)) { out = []; for (let index = 0; index < obj.length; ++index) { const subArray = obj[index]; out.push(Array.isArray(subArray) || typeof subArray === "object" ? cleanJson(subArray) : subArray); } } else if (typeof obj === "object") { out = {}; for (const key in obj) { const subObject = obj[key]; switch (key.charAt(0)) { case "$": case "_": continue; } if (key === "lock") { continue; } out[key] = Array.isArray(subObject) || typeof subObject === "object" ? cleanJson(subObject) : subObject; } } return out; }; cleanJson(obj);
cleanJson (new, delete lock)
const cleanJson = (obj) => { let out = obj; if (Array.isArray(obj)) { out = []; for (let index = 0; index < obj.length; ++index) { const subArray = obj[index]; out.push(Array.isArray(subArray) || typeof subArray === "object" ? cleanJson(subArray) : subArray); } } else if (typeof obj === "object") { out = {}; for (const key in obj) { const subObject = obj[key]; switch (key.charAt(0)) { case "$": case "_": continue; } out[key] = Array.isArray(subObject) || typeof subObject === "object" ? cleanJson(subObject) : subObject; } } return out; }; let newObj = cleanJson(obj); delete newObj.lock;
cleanJson (new, mark lock)
const cleanJson = (obj) => { let out = obj; if (Array.isArray(obj)) { out = []; for (let index = 0; index < obj.length; ++index) { const subArray = obj[index]; out.push(Array.isArray(subArray) || typeof subArray === "object" ? cleanJson(subArray) : subArray); } } else if (typeof obj === "object") { out = {}; for (const key in obj) { const subObject = obj[key]; switch (key.charAt(0)) { case "$": case "_": continue; } out[key] = Array.isArray(subObject) || typeof subObject === "object" ? cleanJson(subObject) : subObject; } } return out; }; let newObj = cleanJson(obj); newObj.lock = undefined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
cleanJson (old)
cleanJson (new, ignore lock)
cleanJson (new, delete lock)
cleanJson (new, mark lock)
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):
The final answer is not explicitly requested, but based on the provided benchmark results, I can infer some general insights. The benchmark test measures the performance of the `cleanJson` function under different conditions: 1. **new, ignore lock**: This variant sets `lock` to `undefined` and ignores it during execution. 2. **new, mark lock**: This variant sets `lock` to a value (not shown in the provided results) and marks it as locked during execution. 3. **new, delete lock**: This variant deletes the `lock` property after creating the new object. The benchmark results show that: * The `cleanJson (new, ignore lock)` variant has the highest executions per second (166,886.52), indicating fast performance with this approach. * The `cleanJson (new, mark lock)` variant has a lower execution rate (164,628.36) compared to ignoring the lock, suggesting that marking the lock adds some overhead. * The `cleanJson (new, delete lock)` variant has the lowest execution rate (153,900.48), which might be due to the additional deletion operation. To make an informed decision about which variant is best, you should consider your specific use case and requirements: * If you want fast performance without worrying about locking mechanisms, use `ignore lock`. * If you prioritize performance over simplicity, use `mark lock`, but be aware of the potential overhead. * If you need to maintain some level of control or consistency with your locking mechanism, use `delete lock`. Keep in mind that these are general insights based on the provided results and might not reflect the optimal solution for your specific scenario.
Related benchmarks:
Delete property from object
cloneDeep vs JSON.*
Lodash cloneDeep vs JSON.stringify + JSON.parse
delete vs spread need for speed
Lodash cloneDeep vs JSON Clone vs Ramda Clone for big Array
Comments
Confirm delete:
Do you really want to delete benchmark?