Created
June 19, 2010 07:25
-
-
Save JosephPecoraro/444674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function buildCSSProperties() { | |
var used = {}; // hash table lookup instead of scanning the list | |
var properties = []; // build the properties ourselves | |
var style = document.documentElement.style; // cache the style, prevent numerous property lookups | |
var list = document.defaultView.getComputedStyle(document.documentElement, ""); | |
var length = list.length; | |
for (var i = 0; i < length; ++i) // fill the hash table early with known values | |
used[properties[i] = list[i]] = true; // combine statements (1) mark as used (2) add to list | |
for (var i = 0, end = length; i < length; ++i) { // keep end pointer to prevent push() calls | |
var propertyWords = properties[i].split("-"); | |
var j = propertyWords.length; | |
while (--j) { | |
propertyWords.pop(); // pop() instead of slice() | |
var shorthand = propertyWords.join("-"); | |
if (!(shorthand in used) && style[shorthand] !== undefined) { // hash lookup first, and no typeof | |
used[shorthand] = true; | |
properties[end++] = shorthand; | |
} | |
} | |
} | |
return properties.sort(); | |
} | |
WebInspector = {}; | |
WebInspector.CSS = {}; | |
WebInspector.CSS.properties = buildCSSProperties(); | |
WebInspector.CSS.properties.startsWith = function startsWith(prefix) | |
{ | |
// FIXME: Use binary search. | |
return this.filter(function(property) { | |
return property.indexOf(prefix) === 0; | |
}); | |
} | |
WebInspector.CSS.properties.startsWith2 = function startsWith2(prefix) | |
{ | |
var firstIndex = this._firstIndexOfPrefix(prefix); | |
if (firstIndex === -1) | |
return []; | |
var results = []; | |
while (this[firstIndex].indexOf(prefix) === 0) | |
results.push(this[firstIndex++]); | |
return results; | |
} | |
WebInspector.CSS.properties._firstIndexOfPrefix = function(prefix) | |
{ | |
if (!prefix) | |
return -1; | |
var maxIndex = this.length - 1; | |
var minIndex = 0; | |
var foundIndex; | |
do { | |
var middleIndex = (maxIndex + minIndex) >> 1; | |
if (this[middleIndex].indexOf(prefix) === 0) { | |
foundIndex = middleIndex; | |
break; | |
} | |
if (this[middleIndex] < prefix) | |
minIndex = middleIndex + 1; | |
else | |
maxIndex = middleIndex - 1; | |
} while (minIndex <= maxIndex); | |
if (!foundIndex) | |
return -1; | |
while (foundIndex && this[foundIndex - 1].indexOf(prefix) === 0) | |
foundIndex--; | |
return foundIndex; | |
} | |
WebInspector.CSS.properties.firstStartsWith = function firstStartsWith(prefix) | |
{ | |
var foundIndex = this._firstIndexOfPrefix(prefix); | |
return (foundIndex === -1 ? "" : this[foundIndex]); | |
} | |
var TOTAL = 10000; | |
var RUN1, RUN2; | |
var STR = "co"; | |
// Time version 1 | |
(function() { | |
var s = Date.now(); | |
console.time("version 1"); | |
for (var i=0; i<TOTAL; ++i) | |
WebInspector.CSS.properties.startsWith(STR); | |
console.timeEnd("version 1"); | |
var e = Date.now(); | |
RUN1 = e-s; | |
})(); | |
// Time version 2 | |
(function() { | |
var s = Date.now(); | |
console.time("version 2"); | |
for (var i=0; i<TOTAL; ++i) | |
WebInspector.CSS.properties.startsWith2(STR); | |
console.timeEnd("version 2"); | |
var e = Date.now(); | |
RUN2 = e-s; | |
})(); | |
console.log( WebInspector.CSS.properties.startsWith(STR) ); | |
console.log( WebInspector.CSS.properties.startsWith2(STR) ); | |
console.log("version 2 is " + (RUN1/RUN2).toFixed(2) + " times faster"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment