Skip to content

Instantly share code, notes, and snippets.

@dmarcelino dmarcelino/testDecode.js
Last active Aug 29, 2015

Embed
What would you like to do?
Decoding OrientDB RID: decodeURIComponent vs string.replace
var n = 5000000;
var encodedRid = '%2310%3A23'; // #10:23
console.time('decodeURIComponent 1/2');
for (var i = 0; i < n; i++) {
decodeURIComponent(encodedRid);
}
console.timeEnd('decodeURIComponent 1/2');
console.time('str.replace 1/2');
for (var i = 0; i < n; i++) {
var res = encodedRid.replace('%23', '#');
res.replace('%3A', ':');
}
console.timeEnd('str.replace 1/2');
console.time('str.replace regex 1/2');
for (var i = 0; i < n; i++) {
var res = encodedRid.replace(/%23/, '#');
res.replace(/%3A/, ':');
}
console.timeEnd('str.replace regex 1/2');
console.time('str.replace regex 2/2');
for (var i = 0; i < n; i++) {
var res = encodedRid.replace(/%23/, '#');
res.replace(/%3A/, ':');
}
console.timeEnd('str.replace regex 2/2');
console.time('str.replace 2/2');
for (var i = 0; i < n; i++) {
var res = encodedRid.replace('%23', '#');
res.replace('%3A', ':');
}
console.timeEnd('str.replace 2/2');
console.time('decodeURIComponent 2/2');
for (var i = 0; i < n; i++) {
decodeURIComponent(encodedRid);
}
console.timeEnd('decodeURIComponent 2/2');
/*
decodeURIComponent 1/2: 1503ms
str.replace 1/2: 1226ms
str.replace regex 1/2: 2482ms
str.replace regex 2/2: 2499ms
str.replace 2/2: 1215ms
decodeURIComponent 2/2: 1506ms
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.