Buffer vs String speed: Why is String faster?

后端 未结 2 1684
忘了有多久
忘了有多久 2020-12-29 05:38

I have this project, called Memcached.Js, which is a port of Memcached server to Node.js.

I\'ve been playing arround with strings and buffers, comparing memory footp

2条回答
  •  别那么骄傲
    2020-12-29 06:13

    Buffer.slice is expensive in node. I've found that the pattern:

    buffer.slice(start, end).toString(encoding) 
    

    was more than 10 times slower than the pattern:

    buffer.toString(encoding, start, end)
    

    Even though slice doesn't allocate any new buffer, it seems to incur a significant cost. From a cursory look at the code, my guess is that exposing the externally allocated buffer to v8 (via SetIndexedPropertiesToExternalArrayData) causes it to have to update its generated code for the buffer object.

    Once created (or sliced), buffers seem fast. So creating bigger buffers instead of lots of little ones, and reusing when possible seems like a reasonable strategy for performance.

    More thoughts on this: http://geochap.wordpress.com/2011/05/03/node-buffers/

提交回复
热议问题