Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
each-or-plain
(version: 8)
Comparing performance of:
each vs for
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Script Preparation code:
var generateName = function(prefix) { return (prefix || '') + Math.random().toString(36).substr(2); }; var generateValue = function(onlyTrivial) { var variants = [ /* real number */ function() { return Math.random(); }, /* integer number */ function() { return Math.floor(Math.random() * 1e16); }, /* string */ function() { return generateName(); }, /* boolean */ function() { return !Math.floor(Math.random() * 2); }, /* array */ function() { var result = []; for (var x = Math.floor(Math.random() * 3); x; x--) { result.push(generateValue()); } return result; }, /* object */ function() { var result = {}; for (var x = Math.floor(Math.random() * 3); x; x--) { result[generateName('x')] = generateValue(); } return result; } ]; if (onlyTrivial) { /* drop array and object variant */ variants.pop(); variants.pop(); } return variants[Math.floor(Math.random() * variants.length)](); }; var generateTree = function() { var args = Array.prototype.slice.call(arguments); var result = {}; var loop = args.shift(); var onlyTrivial = false; if (loop === false || loop === true) { onlyTrivial = loop; loop = args.shift(); } args.unshift(onlyTrivial); for (; loop; loop--) { result[generateName('x')] = (args.length === 1) ? generateValue(onlyTrivial) : generateTree.apply(null, args); } return result; }; var forbiddenElement = /^\[object (DIV|DOM|CSS|HTML|NamedNode|Node|Window)/; var isInvalidElement = function(element) { if (element) { if (element instanceof window.Node) { return true; } if ((element instanceof $) || (typeof element.css === 'function')) { return true; } if ((typeof element.toString === 'function') && forbiddenElement.test(element.toString())) { return true; } } return false; }; var getKeys1 = function(tree, parent, order, skipEmpty) { parent = parent || '/'; var keys = []; var type = $.type(tree); if ((type === 'object') && isInvalidElement(tree)) { type = 'invalid'; } if (type === 'object' || type === 'array') { /* iterate over tree items */ $.each(tree, function(key, value) { var leaf = parent + key; var valueType = $.type(value); keys.push(leaf); /* make sure that invalid elements (like: dom, $, window, etc) won't be parsed */ if ((valueType === 'object') && isInvalidElement(value)) { valueType = 'invalid'; } /* if item is object or array, perform recursive call to get it's keys as well */ if (valueType === 'object' || valueType === 'array') { $.merge(keys, getKeys1(value, leaf + '/', null, skipEmpty)); } }); } return (typeof order === 'function') ? keys.sort(order) : keys.sort(); }; var getKeys2 = function(tree, parent, order, skipEmpty, level) { parent = parent || '/'; level = level || 0; var result; var keys = []; var type = $.type(tree); if ((type === 'object') && isInvalidElement(tree)) { type = 'invalid'; } if (type === 'object' || type === 'array') { /* iterate over tree items */ for (var key in tree) { if (tree.hasOwnProperty(key)) { var value = tree[key]; var leaf = parent + key; var valueType = $.type(value); keys.push(leaf); /* make sure that invalid elements (like: dom, $, window, etc) won't be parsed */ if ((valueType === 'object') && isInvalidElement(value)) { valueType = 'invalid'; } /* if item is object or array, perform recursive call to get it's keys as well */ if (valueType === 'object' || valueType === 'array') { $.merge(keys, getKeys2(value, leaf + '/', null, skipEmpty, level + 1)); } } } } if (level === 0) { result = (typeof order === 'function') ? keys.sort(order) : keys.sort(); } else { result = keys; } return result; };
Tests:
each
var z = window.zzz1; if (!z) { z = generateTree(3, 3, 3, 3, 2, 4); window.zzz1 = z; } getKeys1(z);
for
var z = window.zzz2; if (!z) { z = generateTree(3, 3, 3, 3, 2, 4); window.zzz2 = z; } getKeys2(z);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
each
for
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 the benchmark and its test cases. **Benchmark Definition** The benchmark is defined by two functions: `generateTree` and `getKeys1` (and later, `getKeys2`). The main function that runs the benchmark is not provided in the code snippet. However, based on the test cases, it appears to be a simple assignment followed by a call to either `getKeys1` or `getKeys2`. **Test Cases** There are two individual test cases: 1. **"each"`** The test case uses the `generateTree` function with specific arguments (3, 3, 3, 3, 2, 4) and assigns it to a variable `z`. It then calls `getKeys1(z)` without any additional arguments. 2. **"for"`** This test case is similar to the previous one, but it uses the `generateTree` function with slightly different arguments (3, 3, 3, 3, 2, 4) and assigns it to a variable `z`. It then calls `getKeys2(z)` without any additional arguments. **HTML Preparation Code** The benchmark includes an HTML snippet that loads the jQuery library from Google's CDN. This suggests that the benchmark might be testing JavaScript performance, specifically in relation to DOM manipulation and string processing. **Raw UA String and Device Platform** The raw UA string indicates that both test cases were run on a Chrome 57 browser on Windows. The device platform is listed as "Other". **Benchmark Results** The latest benchmark results show two test cases: 1. **"for"`**: The browser executed the code approximately 244 executions per second. 2. **"each"`**: The browser executed the code approximately 107 executions per second. These results suggest that the `getKeys2` function is faster than `getKeys1`. **$ and Window Object** The presence of `$` (a reference to jQuery) and `window` in the benchmark definition and test cases implies that the benchmark might be testing performance in relation to these objects. This could be due to caching, object creation, or other factors. Overall, this benchmark appears to be testing performance differences between two related functions (`getKeys1` and `getKeys2`) and comparing their execution speeds on a Chrome 57 browser on Windows.
Related benchmarks:
test124578
Array loop vs foreach vs map2
toString vs template literals + array of random strings
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?