我正在用
设置memcached
$memcached->set("item" , ["1" => "hello"]);
任何在 PHP 中工作的东西,
在带有 memcached
插件的 Node.js 中,我在结果中得到一个缓冲区而不是数组
<Buffer 61 3a 25 61 34 3a>
我无法将这样的缓冲区转换为数组
在 Node.js 中:
memcached.get("item" , function(err, data) {
console.log(data);
}
你有什么办法吗?
最佳答案
arr = [...缓冲区]
除了缓冲区之外,ES6 还引入了许多其他特性。
您甚至可以像这样轻松附加:
arr.push(...buffer)
...
运算符在数组中使用时扩展数组和缓冲区等可枚举值。它还将它们扩展为单独的函数参数。
是的,它也更快:
... : x100000: 835.850ms
Slice call from prototype : x100000: 2118.513ms
var array,
buffer = new Buffer([1, 4, 4, 5, 6, 7, 5, 3, 5, 67, 7, 4, 3, 5, 76, 234, 24, 235, 24, 4, 234, 234, 234, 325, 32, 6246, 8, 89, 689, 7687, 56, 54, 643, 32, 213, 2134, 235, 346, 45756, 857, 987, 0790, 89, 57, 5, 32, 423, 54, 6, 765, 65, 745, 4, 34, 543, 43, 3, 3, 3, 34, 3, 63, 63, 35, 7, 537, 35, 75, 754, 7, 23, 234, 43, 6, 247, 35, 54, 745, 767, 5, 3, 2, 2, 6, 7, 32, 3, 56, 346, 4, 32, 32, 3, 4, 45, 5, 34, 45, 43, 43]),
iter = 100000;
array = buffer;
console.time("... : x" + iter);
for (var i = iter; i--;) array = [...buffer]
console.timeEnd("... : x" + iter);
console.time("Apply/call/etc : x" + iter);
for (var i = iter; i--;) array = Array.prototype.slice.call(buffer, 0)
console.timeEnd("Apply/call/etc : x" + iter);
关于javascript - 将缓冲区转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148827/