build-xml-vs-string-replace
JavaScript performance comparison
Preparation code
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
function fnReplaceAllRegEx(value, exp, replace) {
return value.replace(exp, replace);
}
function fnReplaceAll(value, search, replace, ignoreCase) {
if (typeof ignoreCase === "undefined") {
ignoreCase = true;
}
var exp = new RegExp(search, (ignoreCase ? 'gi' : 'g'));
return fnReplaceAllRegEx(value, exp, replace);
}
function buildXmlString(data, parentNodeName, includeXmlHeader) {
if (typeof includeXmlHeader === "undefined") {
includeXmlHeader = true;
}
var ret = '';
var nodeStart;
var nodeEnd;
var nodeSelfClosing;
var propVal;
for (var prop in data) {
propVal = data[prop];
if (_.isFunction(propVal)) {
continue;
}
nodeStart = '<' + prop + '>';
nodeEnd = '</' + prop + '>';
nodeSelfClosing = '<' + prop + ' />';
if (_.isArray(propVal)) {
ret += nodeStart + (propVal).join(nodeEnd + nodeStart) + nodeEnd;
} else {
if (_.isObject(propVal)) {
ret += buildXmlString(propVal, prop);
} else {
if (_.isNull(propVal) || _.isUndefined(propVal) || (_.isString(propVal) && _.isEmpty(propVal))) {
ret += nodeSelfClosing;
} else {
ret += nodeStart + propVal + nodeEnd;
}
}
}
}
if (!_.isEmpty(parentNodeName)) {
ret = '<' + parentNodeName + '>' + ret + '</' + parentNodeName + '>';
}
if (includeXmlHeader) {
ret = '<?xml version="1.0" encoding="UTF-8"?>' + ret;
}
return ret;
}
var sLoginBodyData = '<?xml version="1.0" encoding="UTF-8"?>' + '<loginIPhoneDTO>' +
// ディストリビューター番号 or メールアドレスとパスワード
'<loginId>@sLoginId@</loginId>' +
// パスワード
'<password>@sPassword@</password>' +
// 遷移先URL必須フラグ
'<nextURLRequired>@bNextURLRequired@</nextURLRequired>' +
// ユーザエージェント
'<userAgent>@sUserAgent@</userAgent>' +
// アドレスクリプト
'<tempAddressCrypt>@sTempAddressCrypt@</tempAddressCrypt>' +
// ソフトログインフラグ
'<keepSoft>@sKeepSoft@</keepSoft>' +
// 遷移元URL
'<prevURL>@sPrevURL@</prevURL>' +
// 遷移先URL
'<nextURL>@sNextURL@</nextURL>' + '</loginIPhoneDTO>';
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
builder |
|
pending… |
replacer |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments