From c9d20dbcf25ec06cd0dc7cedfc026c4e62242d73 Mon Sep 17 00:00:00 2001 From: Lucas Serven Date: Mon, 28 Apr 2014 01:01:49 -0400 Subject: [PATCH] Optimize `getElements()` method This commit optimizes the internal `getElements` method, which Postpone uses to locate and register all elements on the page who's resources should be managed. This code change takes advantage of the fact that if an element is already being managed by Postpone and the postponed elements on the page have not changed, then the index of that given element in the array of postponed elements should already be known. This allows for the elimination of the `Array.prototype.indexOf` method, effectively changing the line of code from O(n) to O(1). --- bower.json | 2 +- component.json | 2 +- index.js | 2 +- package.json | 2 +- test/build.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bower.json b/bower.json index e1f6533..13ddd73 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "postpone", "main": "index.js", - "version": "0.6.2", + "version": "0.6.3", "homepage": "https://github.com/lsvx/postpone", "authors": [ "Lucas Serven " diff --git a/component.json b/component.json index d811764..fd860e6 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "postpone", "repo": "lsvx/postpone", "description": "A polyfill for postponing the loading of media.", - "version": "0.6.2", + "version": "0.6.3", "keywords": ["postpone", "media", "resource", "priority", "download"], "dependencies": {}, "development": {}, diff --git a/index.js b/index.js index 369f301..77276f6 100644 --- a/index.js +++ b/index.js @@ -139,7 +139,7 @@ if ( this.isVisible( matches[ i ] ) ) { visible.push( matches[ i ] ); /** Check if this element is not already postponed. */ - if ( !~this.elements.visible.indexOf( matches[ i ] ) ) { + if ( matches[ i ] !== this.elements.visible[ visible.length - 1 ] ) { change = true; } } diff --git a/package.json b/package.json index a4af471..76a554c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postpone", - "version": "0.6.2", + "version": "0.6.3", "description": "A polyfill for postponing the loading of media.", "main": "index.js", "directories": { diff --git a/test/build.js b/test/build.js index 0918455..19da7d2 100644 --- a/test/build.js +++ b/test/build.js @@ -341,7 +341,7 @@ require.register("postpone/index.js", Function("exports, require, module", if ( this.isVisible( matches[ i ] ) ) {\n\ visible.push( matches[ i ] );\n\ /** Check if this element is not already postponed. */\n\ - if ( !~this.elements.visible.indexOf( matches[ i ] ) ) {\n\ + if ( matches[ i ] !== this.elements.visible[ visible.length - 1 ] ) {\n\ change = true;\n\ }\n\ }\n\