Skip to content

Commit

Permalink
Avoid reading outside of collection bounds
Browse files Browse the repository at this point in the history
Consider the following collection:

    const array = ['a', 'b', 'c'];

Retrieving `array[0]` can be done relatively quickly. However, when the property doesn’t exist on the receiver, JavaScript engines must continue to look up the prototype chain until either the property is found or the chain ends. This is inherently slower than *not* doing any prototype chain lookups. Retrieving an out-of-bounds index, e.g. `array[3]`, triggers this scenario, resulting in decreased performance.

This patch changes the way the `cleanData` loop is written to avoid running into the slow case unnecessarily.

A more in-depth explanation can be found here: https://ripsawridge.github.io/articles/blink-mysterium/
  • Loading branch information
mathiasbynens committed Aug 29, 2017
1 parent 692f9d4 commit 2a1564b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ jQuery.extend( {
cleanData: function( elems ) {
var data, elem, type,
special = jQuery.event.special,
length = elems.length,
i = 0;

for ( ; ( elem = elems[ i ] ) !== undefined; i++ ) {
for ( ; i < length; i++ ) {
elem = elems[ i ];
if ( acceptData( elem ) ) {
if ( ( data = elem[ dataPriv.expando ] ) ) {
if ( data.events ) {
Expand Down

0 comments on commit 2a1564b

Please sign in to comment.