Created
June 19, 2010 06:54
-
-
Save JosephPecoraro/444648 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
Array.convert = function convert(list) { | |
return Array.prototype.slice.call(list); | |
} | |
function buildCSSProperties() { | |
var properties = Array.convert(document.defaultView.getComputedStyle(document.documentElement, "")); | |
var length = properties.length; | |
for (var i = 0; i < length; ++i) { | |
var propertyWords = properties[i].split("-"); | |
var j = propertyWords.length; | |
while (--j) { | |
var shorthand = propertyWords.slice(0, j).join("-"); | |
if (typeof document.documentElement.style[shorthand] !== "undefined" && properties.indexOf(shorthand) < 0) | |
properties.push(shorthand); | |
} | |
} | |
return properties.sort(); | |
} | |
function buildCSSProperties2() { | |
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(); | |
} | |
var TOTAL = 1000; | |
var RUN1, RUN2; | |
// Compare Equality | |
var x = buildCSSProperties(); | |
var y = buildCSSProperties2(); | |
for (var i=0; i<x.length; ++i) | |
if (x[i] !== y[i]) | |
console.log('not equal'); | |
if (x.length !== y.length) | |
console.log('not equal'); | |
// Time version 1 | |
(function() { | |
var s = Date.now(); | |
console.time("version 1"); | |
for (var i=0; i<TOTAL; ++i) | |
buildCSSProperties(); | |
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) | |
buildCSSProperties2(); | |
console.timeEnd("version 2"); | |
var e = Date.now(); | |
RUN2 = e-s; | |
})(); | |
// Compare | |
console.log("verison 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