From 2a1564bddffe585fba9b29b315939e1b695cd01f Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Tue, 29 Aug 2017 09:56:03 +0200 Subject: [PATCH] Avoid reading outside of collection bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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/ --- src/manipulation.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/manipulation.js b/src/manipulation.js index 2db1cfe3fd9..5ccd2d9fb50 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -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 ) {