Skip to content

Instantly share code, notes, and snippets.

@1st1
Created January 11, 2020 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1st1/5a7e5a8ff36d49f492631c74bc007515 to your computer and use it in GitHub Desktop.
Save 1st1/5a7e5a8ff36d49f492631c74bc007515 to your computer and use it in GitHub Desktop.
const buf = Buffer.allocUnsafe(16);
function f1(buf) {
return (
buf.slice(0, 4).toString("hex") +
"-" +
buf.slice(4, 6).toString("hex") +
"-" +
buf.slice(6, 8).toString("hex") +
"-" +
buf.slice(8, 10).toString("hex") +
"-" +
buf.slice(10, 16).toString("hex")
);
}
function f2(buf) {
return [
buf.slice(0, 4).toString("hex"),
buf.slice(4, 6).toString("hex"),
buf.slice(6, 8).toString("hex"),
buf.slice(8, 10).toString("hex"),
buf.slice(10, 16).toString("hex"),
].join("-");
}
function f3(buf) {
const sl = buf.toString("hex");
return [
sl.slice(0, 8),
sl.slice(8, 12),
sl.slice(12, 16),
sl.slice(16, 20),
sl.slice(20, 32),
].join("-");
}
function f4(buf) {
const sl = buf.toString("hex");
return (
sl.slice(0, 8) +
"-" +
sl.slice(8, 12) +
"-" +
sl.slice(12, 16) +
"-" +
sl.slice(16, 20) +
"-" +
sl.slice(20, 32)
);
}
function f5(buf) {
const sl = buf.toString("hex");
return sl;
}
console.log(f1(buf));
console.log(f2(buf));
console.log(f3(buf));
console.log(f4(buf));
console.log(f5(buf));
console.time("f1");
for (i = 0; i < 1000000; i++) {
f1(buf);
}
console.timeEnd("f1");
console.time("f2");
for (i = 0; i < 1000000; i++) {
f2(buf);
}
console.timeEnd("f2");
console.time("f3");
for (i = 0; i < 1000000; i++) {
f3(buf);
}
console.timeEnd("f3");
console.time("f4");
for (i = 0; i < 1000000; i++) {
f4(buf);
}
console.timeEnd("f4");
console.time("f5");
for (i = 0; i < 1000000; i++) {
f5(buf);
}
console.timeEnd("f5");
@1st1
Copy link
Author

1st1 commented Jan 11, 2020

nodejs 10 output:

00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
00000000000000000000000000000000
f1: 1060.335ms
f2: 1219.683ms
f3: 408.617ms
f4: 246.338ms
f5: 149.422ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment