-
-
Save monolithed/8674349 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
/** | |
* User Timing polyfill (http://www.w3.org/TR/user-timing/) | |
* @author RubaXa <[email protected]> | |
*/ | |
(function (window){ | |
var | |
startOffset = Date.now ? Date.now() : +(new Date) | |
, performance = window.performance || {} | |
, _entries = [] | |
, _marksIndex = {} | |
, _filterEntries = function (key, value){ | |
var i = 0, n = _entries.length, result = []; | |
for( ; i < n; i++ ){ | |
if( _entries[i][key] == value ){ | |
result.push(_entries[i]); | |
} | |
} | |
return result; | |
} | |
, _clearEntries = function (type, name){ | |
var i = _entries.length, entry; | |
while( i-- ){ | |
entry = _entries[i]; | |
if( entry.entryType == type && (name === void 0 || entry.name == name) ){ | |
_entries.splice(i, 1); | |
} | |
} | |
} | |
; | |
if( !performance.now ){ | |
performance.now = performance.webkitNow || performance.mozNow || performance.msNow || function (){ | |
return (Date.now ? Date.now() : +(new Date)) - startOffset; | |
}; | |
} | |
if( !performance.mark ){ | |
performance.mark = performance.webkitMark || function (name){ | |
var mark = { | |
name: name | |
, entryType: 'mark' | |
, startTime: performance.now() | |
, duration: 0 | |
}; | |
_entries.push(mark); | |
_marksIndex[name] = mark; | |
}; | |
} | |
if( !performance.measure ){ | |
performance.measure = performance.webkitMeasure || function (name, startMark, endMark){ | |
startMark = _marksIndex[startMark].startTime; | |
endMark = _marksIndex[endMark].startTime; | |
_entries.push({ | |
name: name | |
, entryType: 'measure' | |
, startTime: startMark | |
, duration: endMark - startMark | |
}); | |
}; | |
} | |
if( !performance.getEntriesByType ){ | |
performance.getEntriesByType = performance.webkitGetEntriesByType || function (type){ | |
return _filterEntries('entryType', type); | |
}; | |
} | |
if( !performance.getEntriesByName ){ | |
performance.getEntriesByName = performance.webkitGetEntriesByName || function (name){ | |
return _filterEntries('name', name); | |
}; | |
} | |
if( !performance.clearMarks ){ | |
performance.clearMarks = performance.webkitClearMarks || function (name){ | |
_clearEntries('mark', name); | |
}; | |
} | |
if( !performance.clearMeasures ){ | |
performance.clearMeasures = performance.webkitClearMeasures || function (name){ | |
_clearEntries('measure', name); | |
}; | |
} | |
// exports | |
window.performance = performance; | |
})(window); |
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 (){ | |
module('performance'); | |
function sleep(delay){ | |
var start = performance.now(); | |
while( true ){ | |
if( performance.now() - start >= delay ){ | |
break; | |
} | |
} | |
} | |
test('now', function (){ | |
var start = performance.now(); | |
sleep(1); | |
ok(performance.now() - start < 1.5); | |
}); | |
test('performance.mark', function (){ | |
performance.mark("foo"); | |
sleep(5); | |
performance.mark("bar"); | |
sleep(10); | |
performance.mark("baz"); | |
equal(performance.getEntriesByType('mark').length, 3); | |
ok(performance.getEntriesByName('baz')[0].startTime - performance.getEntriesByName('bar')[0].startTime < 11, 'baz - bar'); | |
ok(performance.getEntriesByName('bar')[0].startTime - performance.getEntriesByName('foo')[0].startTime < 6, 'bar - foo'); | |
performance.clearMarks('bar'); | |
equal(performance.getEntriesByType('mark').length, 2); | |
equal(performance.getEntriesByName('foo').length, 1); | |
equal(performance.getEntriesByName('bar').length, 0); | |
equal(performance.getEntriesByName('baz').length, 1); | |
performance.clearMarks(); | |
equal(performance.getEntriesByType('mark').length, 0); | |
}); | |
test('performance.measure', function (){ | |
performance.mark("foo"); | |
sleep(5); | |
performance.mark("bar"); | |
performance.measure('foo_bar', 'foo', 'bar'); | |
equal(performance.getEntriesByType('measure').length, 1); | |
ok(performance.getEntriesByName('foo_bar')[0].duration < 6, performance.getEntriesByName('foo_bar')[0].duration+''); | |
performance.mark("foo"); | |
sleep(5); | |
performance.mark("bar"); | |
sleep(5); | |
performance.mark("foo"); | |
performance.measure('foo_bar', 'foo', 'bar'); | |
equal(performance.getEntriesByType('measure').length, 2); | |
ok(performance.getEntriesByName('foo_bar')[1].duration < -5, performance.getEntriesByName('foo_bar')[1].duration+''); | |
performance.clearMarks(); | |
performance.clearMeasures(); | |
equal(performance.getEntriesByType('measure').length, 0); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment