Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active September 25, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/ee7433f622577872d2e264d04ba2b833 to your computer and use it in GitHub Desktop.
Save nolanlawson/ee7433f622577872d2e264d04ba2b833 to your computer and use it in GitHub Desktop.
Audio benchmark
<!doctype html>
<html>
<head>
<title>Audio benchmark</title>
</head>
<body>
<h1>Audio benchmark</h1>
<button type='button'>Click me to play a sound</button>
<pre></pre>
<script>
// preload audio
let preloaded = new Audio('boop.mp3')
preloaded.volume = 0
preloaded.play()
document.querySelector('button').addEventListener('click', () => {
performance.clearMarks()
performance.clearMeasures()
performance.mark('create:audio')
let audio = new Audio('boop.mp3')
performance.mark('play:audio')
audio.play()
performance.mark('end:audio')
performance.measure('audio', 'create:audio', 'end:audio')
document.querySelector('pre').innerHTML += 'Duration: ' + performance.getEntriesByType('measure').slice(-1)[0].duration + 'ms\n'
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment