handlebars each vs concatenation vs array join
JavaScript performance comparison
Preparation code
<script src="//cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js">
</script>
<script src="//underscorejs.org/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
var optionTemplateString = '{{#each options}}<option value="{{value}}">{{label}}</option>{{/each}}';
var optionTemplate = Handlebars.compile(optionTemplateString);
target = {
options: []
};
resultRegex = /^<option.*<\/option>$/;
var pad = function(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
}
var suffix = '';
for (var i = 0; i < 1000; i++) {
suffix = pad(i, 5);
target.options.push({
value: 'foo' + suffix,
label: 'bar' + suffix
});
}
};
Benchmark.prototype.teardown = function() {
if (!resultRegex.test(result)) {
throw new Error('Unexpected output');
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Handlebars each |
|
pending… |
String concatenation |
|
pending… |
Array join |
|
pending… |
Underscore each |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
- Revision 2: published
- Revision 3: published
- Revision 4: published by Alpha
- Revision 5: published by Alpha
0 comments