Last active
August 29, 2015 14:01
-
-
Save ixti/7b91174d61c53f06c435 to your computer and use it in GitHub Desktop.
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 suite = new (require("benchmark").Suite); | |
var stoper = '<~_#@!'; | |
var stoperRE = /<~_#@!/g; | |
function format(msg, arg) { | |
return msg + ":" + arg; | |
} | |
function varify_puzrin(phrase, params) { | |
// quick-check for speed | |
if (phrase.indexOf('#{') === -1) { return phrase; } | |
// 1. Replace \#{ escaped case with sequence, that will never heppen. | |
// That's a dirty hack, but it works & simplifies code. | |
// 2. Replace #{variables} with values | |
// 3. Return back escaped sequence & unescape it. | |
return phrase | |
.replace(/\\#{/g, stoper) | |
.replace(/#{(.+?)}/g, function (__, name) { | |
return (params[name] !== undefined) ? | |
params[name] : format('[missed variable: %s]', name); | |
}) | |
.replace(stoperRE, '#{'); | |
} | |
function varify_puzrin_2(phrase, params) { | |
// quick-check for speed | |
if (phrase.indexOf('#{') === -1) { return phrase; } | |
// 1. Replace \#{ escaped case with sequence, that will never heppen. | |
// That's a dirty hack, but it works & simplifies code. | |
// 2. Replace #{variables} with values | |
// 3. Return back escaped sequence & unescape it. | |
return phrase | |
.replace(/\\#{/g, stoper) | |
.replace(/#{([^}]+)}/g, function (__, name) { | |
return (params[name] !== undefined) ? | |
params[name] : format('[missed variable: %s]', name); | |
}) | |
.replace(stoperRE, '#{'); | |
} | |
function varify_ixti(phrase, params) { | |
// quick-check for speed | |
if (phrase.indexOf('#{') === -1) { return phrase; } | |
return phrase.replace(/(\\?)#{(.+?)}/g, function (m, esc, name) { | |
if (esc) { return m; } | |
if (undefined !== params[name]) { return params[name]; } | |
return format('[missed variable: %s]', name); | |
}); | |
} | |
function varify_ixti_2(phrase, params) { | |
// quick-check for speed | |
if (phrase.indexOf('#{') === -1) { return phrase; } | |
return phrase.replace(/(\\?)#{([^}]+)}/g, function (m, esc, name) { | |
if (esc) { return m; } | |
if (undefined !== params[name]) { return params[name]; } | |
return format('[missed variable: %s]', name); | |
}); | |
} | |
var str_1 = "foo #{foo} \\#{foo}"; | |
var str_2 = "foo #{foo} \\#{foo} #{bar}"; | |
var str_3 = "foo #{foo} \\#{foo} #{bar} \\#{bar}"; | |
suite.add("varify_puzrin", function() { | |
varify_puzrin(str_1, { foo: "bar", bar: "foo" }); | |
varify_puzrin(str_2, { foo: "bar", bar: "foo" }); | |
varify_puzrin(str_3, { foo: "bar", bar: "foo" }); | |
}); | |
suite.add("varify_puzrin_2", function() { | |
varify_puzrin_2(str_1, { foo: "bar", bar: "foo" }); | |
varify_puzrin_2(str_2, { foo: "bar", bar: "foo" }); | |
varify_puzrin_2(str_3, { foo: "bar", bar: "foo" }); | |
}); | |
suite.add("varify_ixti", function () { | |
varify_ixti(str_1, { foo: "bar", bar: "foo" }); | |
varify_ixti(str_2, { foo: "bar", bar: "foo" }); | |
varify_ixti(str_3, { foo: "bar", bar: "foo" }); | |
}); | |
suite.add("varify_ixti_2", function () { | |
varify_ixti_2(str_1, { foo: "bar", bar: "foo" }); | |
varify_ixti_2(str_2, { foo: "bar", bar: "foo" }); | |
varify_ixti_2(str_3, { foo: "bar", bar: "foo" }); | |
}); | |
suite.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}); | |
suite.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}); | |
suite.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment