2
console.time('#1');
b = Buffer.alloc(100);

for (var ctr = 0; ctr < b.length; ctr++) {
    b[ctr] = 65;
}
console.timeEnd('#1');

TIME: 0.213ms

console.time('#2');
let result = '';
for (var ctr = 0; ctr < 100; ctr++) {
    result += String.fromCharCode(65);
}
console.timeEnd('#2');

TIME: 0.121ms

I would have thought #1 -- the buffer change -- would have been faster. I'm really surprised and am wondering why appending to a string is faster?

ADDENDUM:

Removing Buffer.alloc from test.

b = Buffer.alloc(100);

console.time('#1');
for (var ctr = 0; ctr < b.length; ctr++) {
    b[ctr] = 65;
}
console.timeEnd('#1');

TIME: 0.136ms

Removing .length from test and setting a hard 100.

b = Buffer.alloc(100);

console.time('#1b');
for (var ctr = 0; ctr < 100; ctr++) {
    b[ctr] = 65;
}
console.timeEnd('#1b');

TIME: 0.117ms

console.time('#2');
let result = '';
for (var ctr = 0; ctr < 100; ctr++) {
    result += String.fromCharCode(65);
}
console.timeEnd('#2');

TIME: 0.125ms

It seems that both adjusting "Buffer.alloc" (which was expected) and ".length" added to execution times. Interesting exercise.

2
  • 1
    My first guess would be some big JS compiler optimizations that apply to string building in a loop, but not buffer value assignment. Also, note that b[ctr] = 'a'; doesn't appear to work. You need to put an integer value in the buffer when accessing it that way. Commented Jul 30, 2020 at 2:45
  • Yes, thank you ... I adjusted the code. The time remains constant post-change. Commented Jul 30, 2020 at 3:14

1 Answer 1

2

Remember that node.js is built on Google V8, which was coded with c++. Buffer and String both allocate memory... but buffer is built for raw bytes. It would not surprise me to learn that buffer allocates a straight up c/c++ array of byte and javascript string actually becomes a c++ std::string object on the back end. Individual operations within the allocated memory would prolly be faster on buffer... but when it comes to dynamic size of the string/buffer the performance benefit would lean towards string because the c++ std::string can resize memory requirements on the fly whereas an array has to fully allocate new memory every time.

What would be really interesting is if you allocate the buffer first and declare the string first... then just run the times on the loop operations...

Sign up to request clarification or add additional context in comments.

1 Comment

Your logic doesn't really make sense here. The Buffer is fully allocated once so will require no reallocations. The string is not pre-allocated so, presumably may require some reallocations, yet the string case is lots faster. That can't be the explanation. There's something else going on here. My guess is in a comment to the question (that it's about string building optimizations built into the V8 compiler that do not apply to Buffer manipulations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.