Skip to content

Commit

Permalink
Optimize getElements() method
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
squat committed Apr 28, 2014
1 parent 7dade5e commit c9d20db
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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 <lserven@gmail.com>"
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down

0 comments on commit c9d20db

Please sign in to comment.