<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<style>
#container { visibility: hidden; }
</style>
<div id="container">
</div>
var container = $('#container')
var itemCount = 300;
for (var i = 0; i < itemCount; i++) {
container.append('<div>Item ' + i + '</div>');
}
var items = container.children();
items.each(function(index,item) {
var $item = $(item);
$item.on('click', function() { console.log('Item clicked'); });
$item.on('hover', function() { console.log('Item hovered'); });
$item.on('mouseenter', function() { console.log('Item mouseentered'); });
});
items.each(function(index,item) {
var $item = $(item);
$item.on('click', function() { console.log('Item clicked'); });
});
items.on('click', function() { console.log('Item clicked'); });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.each loop and multiple on-assignenements | |
.each loop and single on-assignement | |
no loop and .on-assignement on list |
Test name | Executions per second |
---|---|
.each loop and multiple on-assignenements | 17.1 Ops/sec |
.each loop and single on-assignement | 178.4 Ops/sec |
no loop and .on-assignement on list | 30.7 Ops/sec |
Measuring JavaScript performance is a complex task, as it depends on various factors such as the specific use case, hardware, and software configurations.
The provided benchmark definition json represents a test for jQuery event handling performance. The script preparation code creates an HTML container with 300 child elements, which are then wrapped by the jQuery library. This setup allows us to measure how efficiently the browser can handle multiple events on each element.
Here's a breakdown of the options being compared:
.on-assignment
: In this test, we attach only one event listener to each element using .on()
. The items.each()
function iterates over the elements, but it does not modify them. This approach is simple and might be faster because it reduces memory allocations..on-assignments
: In this test, we attach multiple events listeners to each element using .on()
, one for click, hover, and mouseenter events. This approach simulates a more complex scenario where elements have multiple handlers attached..on-assignment on list
: In this test, we directly assign event listeners to the items
list without using an iterator or a loop. This approach is likely faster because it avoids the overhead of iterating over elements.Now, let's discuss the pros and cons of each approach:
.on-assignment
:.on-assignments
:.on-assignment on list
:In terms of library usage, jQuery provides several functions for event handling, including:
Other considerations when writing performance-critical JavaScript code include:
requestAnimationFrame()
instead of setInterval()
for animations and updates.For alternatives to MeasureThat.net, consider the following options:
These alternatives offer varying degrees of complexity and customization options, so be sure to review their documentation and choose the one that best fits your needs.