Supplant, concat, array-join
JavaScript performance comparison
Info
Test of string construction by 3 techniques: - Basic concatenation - Using "supplant" and a template - Using array join.
I've heard different stories about these techniques, and how recent Javascript efficiencies have changed things. Thought I'd build a test to check it all out.
Preparation code
<script type="text/javascript">
String.prototype.supplant = function (o) {
return this.replace(
/\{([^{}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
var count = 12;
var base_template = "{a} ";
var template = "";
var letter = 'b';
var dict = { a: 'b' };
for (var n = 0; n < count; n++) {
template = template + base_template;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
supplant from remedial Javascript |
|
pending… |
string concat |
|
pending… |
array join |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments