Created
December 16, 2012 00:35
-
-
Save DigiTec/4301479 to your computer and use it in GitHub Desktop.
Utilize the new PerformanceResourceTiming specification in order to figure out programmatically which resources in your website are misnamed by case only. This can help reduce redundant requests. This is a first crack at the API and only limited testing has been done. There may be URL canonicalization steps that could be made and restriction of …
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
"use strict"; | |
Object.defineProperties(this, { | |
"ResourceTimingArbiter": { | |
value: (function () { | |
var _oldResources = []; | |
var _allDupes = []; | |
var _resourceArbiter = {}; | |
var _escape = function(str) { | |
return "".replace.call(str, /[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); | |
}; | |
var _processDupes = function processDupes(checkSet, baseSet) { | |
var dupes = []; | |
for(var baseIndex = 0; baseIndex < baseSet.length; baseIndex++) { | |
var resourceTiming = baseSet[baseIndex]; | |
if (!resourceTiming._ciRegex) { | |
resourceTiming._ciRegex = new RegExp("^" + _escape(resourceTiming.name) + "$", "i"); | |
} | |
for(var checkIndex = 0; checkIndex < checkSet.length; checkIndex++) { | |
var checkTiming = checkSet[checkIndex]; | |
if (checkTiming !== resourceTiming && !checkTiming.dupedTo && resourceTiming._ciRegex.test(checkTiming.name)) { | |
checkTiming.dupedTo = resourceTiming; | |
_allDupes.push(checkTiming); | |
} | |
} | |
} | |
return dupes; | |
} | |
if (typeof(PerformanceResourceTiming) !== "undefined") { | |
Object.defineProperties(_resourceArbiter, { | |
getDuplicateResources: { | |
value: function getDuplicateResources() { | |
var newResources = performance.getEntriesByType("resource"); | |
performance.clearResourceTimings(); | |
_processDupes(newResources, newResources); | |
_processDupes(newResources, _oldResources); | |
_oldResources = _oldResources.concat(newResources); | |
return _allDupes.map(function (elem) { return elem; }); | |
} | |
}, | |
dumpAllResourcesToConsole: { | |
value: function dumpAllResourcesToConsole() { | |
// Ensure all resources are in _oldResources; | |
this.getDuplicateResources(); | |
for(var resIndex = 0; resIndex < _oldResources.length; resIndex++) { | |
var resourceTiming = _oldResources[resIndex]; | |
window.console.log("Resource: ", resourceTiming.name); | |
window.console.log("\tInitiator: ", resourceTiming.initiatorType); | |
window.console.log("\tDuplicate: ", (resourceTiming.dupedTo !== undefined)); | |
} | |
} | |
}, | |
dumpAllDupesToConsole: { | |
value: function dumpAllDupesToConsole() { | |
var allDupes = this.getDuplicateResources(); | |
for(var dupeIndex = 0; dupeIndex < allDupes.length; dupeIndex++) { | |
var dupe = allDupes[dupeIndex]; | |
window.console.log("Duplicate resource: ", dupe.name, " --> ", dupe.dupedTo.name); | |
} | |
} | |
}, | |
isSupported: { | |
value: true | |
} | |
}); | |
} | |
else { | |
Object.defineProperties(_resourceArbiter, { | |
isSupported: { | |
value: false | |
} | |
}); | |
} | |
return _resourceArbiter; | |
})() | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment