Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Nesting Variants
(version: 0)
Comparing performance of:
Nestie (unsorted) vs Nestie (sorted)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function iter2(output, nullish, sep, val, key) { var k, pfx = key ? key + sep : key; if (val == null) { if (nullish){ data = {}; data[key] = val; // output.push(key || 'root'); } } else if (typeof val != 'object' || Array.isArray(val)) { data = {}; data[key] = val; // output.push(key || 'root'); } else { data = {}; data[key] = val; // output.push(key || 'root'); for (k in val) { iter2(output, nullish, sep, val[k], pfx + k); } } output.push(key || 'root'); } function flattieArray(input, glue, toNull) { var output = []; if (typeof input == 'object') { iter2(output, !!toNull, glue || '.', input, ''); } return output; } function iter(output, nullish, sep, val, key) { var k, pfx = key ? key + sep : key; if (val == null) { if (nullish) output[key] = val; } else if (typeof val != 'object' || Array.isArray(val)) { output[key] = val; } else { for (k in val) { iter(output, nullish, sep, val[k], pfx + k); } } } function flattie(input, glue, toNull) { var output = {}; if (typeof input == 'object') { iter(output, !!toNull, glue || '.', input, ''); } return output; } function empty(key) { var char = key.charCodeAt(0); return char > 47 && char < 58 ? [] : {}; } function nestie(input, glue) { glue = glue || '.'; var arr, tmp, output; var i = 0, k, key; for (k in input) { tmp = output; // reset arr = k.split(glue); // console.log('key',k); for (i = 0; i < arr.length; ) { key = arr[i++]; if (tmp == null) { tmp = empty('' + key); output = output || tmp; } // console.log('key inner', key, tmp); if (key == '__proto__' || key == 'constructor' || key == 'prototype') break; if (i < arr.length) { if (key in tmp) { // console.log('key inner', key, JSON.stringify(tmp[key],null,2)); if (typeof tmp[key] !== 'object') { var ok = tmp[key]; tmp[key] = empty('' + arr[i]); tmp[key]['_'+key] = ok; } tmp = tmp[key]; } else { tmp = tmp[key] = empty('' + arr[i]); } } else { tmp[key] = input[k]; } } } return output; } function nestie2(input, glue) { glue = glue || '.'; var arr, tmp, output; var i = 0, k, key; var kz = Object.keys(input).sort(); //console.log('keys',JSON.stringify(kz,null,2)); var kl = kz.length; for (let index = 0; index < kl; index++) { let k = kz[index]; let v = input[k]; tmp = output; // reset arr = k.split(glue); for (i = 0; i < arr.length; ) { key = arr[i++]; if (tmp == null) { tmp = empty('' + key); output = output || tmp; } //console.log('key2',k,key,tmp); if (key == '__proto__' || key == 'constructor' || key == 'prototype') break; if (i < arr.length) { if (key in tmp) { if (typeof tmp[key] !== 'object') { var ok = tmp[key]; tmp[key] = empty('' + arr[i]); tmp[key]['_'+key] = ok; tmp = tmp[key]; }else{ tmp = tmp[key]; } } else { tmp = tmp[key] = empty('' + arr[i]); } } else { if (key in tmp) { tmp[key]['_'] = v; }else{ tmp[key] = v; } } } } return output; } var data = {x:{y:1},foo:'bar',products: [1, 2, { three: '3' }], page: { category: { l1: 'foo', l2: 'bar' } } }; var flat = flattie(data); flat['page.category'] = 'helloworld'; flat['user'] = { a: 'b' }; flat['foo'] = 'bar'; flat['page.category.l1'] = 'whaaat'; flat['products'] = ['a', 'b']; flat['foo.bar'] = {'hellow':'orld'};
Tests:
Nestie (unsorted)
var exploded = nestie(flat);
Nestie (sorted)
var exploded2 = nestie2(flat);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Nestie (unsorted)
Nestie (sorted)
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):
Measuring performance differences between various approaches is crucial in software development, and MeasureThat.net provides a great platform for doing so. **Benchmark Definition** The provided JSON defines two benchmark test cases: `Nestie (unsorted)` and `Nestie (sorted)`. The benchmark definitions involve calling the `nestie` function on a flat object (`flat`) with different sorting options. In essence, the tests are comparing the performance of the `nestie` function when it is called on an unsorted vs. a sorted input. **Individual Test Cases** The individual test cases are: 1. **Nestie (unsorted)**: Calls `nestie(flat)` without any sorting option. 2. **Nestie (sorted)**: Calls `nestie2(flat)`, which sorts the input keys before calling `nestie`. **Latest Benchmark Result** The latest benchmark result shows two test cases with their respective execution frequencies per second. **Performance Comparison** To understand the performance differences, let's analyze the code snippets: 1. **Nestie (unsorted)**: This version of `nestie` iterates through the input object without any sorting. It uses a simple loop to access each key and performs a lookup in the `tmp` object. 2. **Nestie (sorted)**: This version sorts the input keys before calling `nestie`. The sorting is done using `Object.keys()` and `sort()`, which creates an array of sorted keys. The performance difference between these two approaches is likely due to the additional overhead introduced by sorting the input keys. Sorting requires iterating through the entire key space, creating a temporary data structure (the sorted array), and then reiterating through it to access each key. **Expected Results** Based on this analysis, I would expect: * **Nestie (unsorted)**: To have higher execution frequencies per second, since it avoids the additional overhead of sorting. * **Nestie (sorted)**: To have lower execution frequencies per second due to the increased complexity introduced by sorting. However, without seeing the actual implementation details of `nestie`, it's difficult to predict the exact performance difference. MeasureThat.net will provide more insights into the actual performance characteristics of these approaches.
Related benchmarks:
lodash flatten vs spread not broken
flatmap
Flatten array
lodash flatten vs concat
Lodash flattern vs flat
Comments
Confirm delete:
Do you really want to delete benchmark?