Last active
August 29, 2015 14:04
-
-
Save jinroh/035e62b422f49edde890 to your computer and use it in GitHub Desktop.
ISML Parsing Bench
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
var _ = require('lodash'); | |
var Promise = require('bluebird'); | |
var sax = require('sax'); | |
var manifest = require("raw!./manifest.xml"); | |
function domParser() { | |
function parseStreamIndex(el) { | |
var index = []; | |
if (!el) { return; } | |
var cdren = el.children; | |
if (!cdren) { return; } | |
for (var i = 0; i < cdren.length; i++) { | |
index.push({ d: +cdren[i].getAttribute('d'), c: +cdren[i].getAttribute('c') }); | |
} | |
return index; | |
} | |
var xml = new DOMParser().parseFromString(manifest, 'application/xml'); | |
var sidx = xml.getElementsByTagName('StreamIndex'); | |
var indexes = []; | |
for (var i = 0; i < sidx.length; i++) { | |
indexes.push(parseStreamIndex(sidx[i])); | |
} | |
return indexes; | |
} | |
function lodParser() { | |
var xml = new DOMParser().parseFromString(manifest, "application/xml"); | |
return _.map(xml.getElementsByTagName('StreamIndex'), function(el) { | |
return _.map(el.children, (c) => ({ d: +c.getAttribute('d'), c: +c.getAttribute('c')})); | |
}); | |
} | |
function saxParser() { | |
return new Promise(function(done, fail) { | |
var parser = sax.parser(true /* strict */); | |
var indexes = []; | |
var index; | |
parser.onopentag = (node) => { | |
if (node.name === 'StreamIndex') { | |
index = []; | |
} else if (node.name === 'c') { | |
index.push(node.attributes); | |
} | |
}; | |
parser.onclosetag = (node) => { | |
if (node === 'StreamIndex') { | |
indexes.push(index); | |
} | |
}; | |
parser.onerror = (err) => fail(err); | |
parser.onend = () => done(indexes); | |
parser.write(manifest).close(); | |
}); | |
} | |
function xhrParser() { | |
return new Promise(function(done) { | |
var xhr = new XMLHttpRequest(); | |
var url = "http://mediacenter.apps.canallabs.fr/live/hss/canalplus-hd.isml/manifest"; | |
xhr.open('GET', url); | |
xhr.setRequestHeader('X-Requested-With', 'hss-live-m1-l3.canal-plus.com'); | |
xhr.onload = (e) => { | |
var xml = e.target.responseXML; | |
done(_.map(xml.getElementsByTagName('StreamIndex'), (el) => { | |
return _.map(el.children, (c) => ({ d: +c.getAttribute('d'), c: +c.getAttribute('c') })); | |
})); | |
}; | |
xhr.send(); | |
}); | |
} | |
function bench(name, fn, num) { | |
var now = Date.now(); | |
return Promise.reduce(_.range(0, num), fn, 0).then(() => { | |
var tot = Date.now() - now; | |
document.body.innerHTML += `<p>${name} (${num}): ${tot / num}</p>`; | |
return 1; | |
}); | |
} | |
document.body.style.marginLeft = "100px"; | |
document.body.style.backgroundColor = "red"; | |
document.body.style.fontFamily = "courier"; | |
document.body.style.fontSize = "50px"; | |
Promise.resolve() | |
.then(() => bench('dom', domParser, 100)).delay(500) | |
.then(() => bench('lod', lodParser, 100)).delay(500) | |
.then(() => bench('sax', saxParser, 100)).delay(500) | |
.then(() => bench('xhr', xhrParser, 10)).delay(500); | |
// Bench (in ms) | |
// +-------------------------------------------+ | |
// | | chrome | stb (tvhd) | stb (notv) | | |
// | dom | 148.1 | 19,239 | 10,657 | | |
// | lodash | 128.4 | 18,432 | 10,186 | | |
// | sax | 603.6 | 121,911 | 55,968 | | |
// | | | | | | |
// | xhr | 4,183 | 22,132 | 12,350 | | |
// +-------------------------------------------+ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment