Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
inner / outer
(version: 0)
Comparing performance of:
inner vs outer vs bind vs let
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<body> <a href="//www.yahoo.com" target="_blank">Yahoo!</a><br/> <a href="//www.altavista.com" target="_blank">AltaVista</a><br/> <a href="//www.google.com" target="_blank">Google</a><br/> </body>
Tests:
inner
(function registerHandlers() { var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { anchors[i].onclick = (function(idx) { return function() { alert(idx); return false; } })(i); } })();
outer
(function registerHandlers() { var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { anchors[i].onclick = bla.bind(null, i); } function bla(i) { alert(i); return false; } })();
bind
(function registerHandlers() { var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { anchors[i].onclick = function(i) { alert(i); return false; }.bind(null, i); } })();
let
(function registerHandlers() { var anchors = document.getElementsByTagName('a'); for (let i = 0; i < anchors.length; i++) { anchors[i].onclick = function() { alert(i); return false; } } })();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
inner
outer
bind
let
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 dive into the benchmark and explore what's being tested. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests different approaches to setting an event handler for HTML anchors (`<a>` tags). The benchmark consists of four test cases: 1. **inner**: This test case sets the `onclick` attribute directly on the anchor element. 2. **outer**: This test case uses the `bind()` method to set the `onclick` attribute, passing a function that takes an argument (`i`) and returns a value. 3. **bind**: Similar to the "outer" test case, but using `bind()` with no arguments (i.e., binding the function to `null`). 4. **let**: This test case uses the `let` keyword to declare a variable `i` within the scope of the loop. **Options Compared** The benchmark compares the performance of these four approaches: 1. Setting the `onclick` attribute directly on the anchor element (`inner`) 2. Using the `bind()` method with an argument (outer) 3. Using the `bind()` method without arguments (bind) 4. Using the `let` keyword to declare a variable within the scope of the loop (let) **Pros and Cons** Here's a brief summary of each approach: 1. **inner**: Simple, straightforward, but potentially slower due to the overhead of creating a new function on every iteration. 2. **outer**: Uses the `bind()` method to create a new function that captures the value of `i` at each iteration. This approach is likely faster than `inner`, but may incur additional overhead due to the creation of a new function. 3. **bind**: Similar to "outer", but without providing an argument to `bind()`. This approach is likely slower than "outer" since it doesn't capture the value of `i`. 4. **let**: Uses the `let` keyword to declare a variable within the scope of the loop, which can lead to faster execution times due to reduced overhead. **Library and Purpose** None of the benchmark test cases use any external libraries. The only library-like functionality is the `bind()` method, which is a built-in JavaScript function used for functional programming. **Special JS Feature or Syntax** The "outer" and "bind" test cases use the `bind()` method with an argument (or without an argument), which is a feature of modern JavaScript that allows functions to be bound to specific contexts. The "let" test case uses the `let` keyword, which was introduced in ECMAScript 2015. **Other Alternatives** In theory, there are other approaches to setting event handlers for anchors that could be used as alternatives: 1. Using an arrow function (e.g., `(i) => { alert(i); return false; }`) 2. Using a closure (e.g., `function() { let i = 0; return function() { alert(i++); return false; }; }`) 3. Using a different library or framework that provides optimized event handling mechanisms. However, these alternatives may not be as widely supported or efficient as the approaches used in the benchmark test cases.
Related benchmarks:
inner / outer
Hide element
element hide
Inspecting the dom
Comments
Confirm delete:
Do you really want to delete benchmark?