将缓冲区转换为数组 [英] Convert buffer to array

查看:151
本文介绍了将缓冲区转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将

$memcached->set("item" , ["1" => "hello"]);

PHP中的所有工作,

anything work in PHP ,

在具有memcached插件的Node.js中,我在结果中得到一个缓冲区而不是数组

In Node.js with memcached plugin , I get a buffer instead of array in result

<Buffer 61 3a 25 61 34 3a>

我无法将此类缓冲区转换为数组

I can not convert such buffer to array

在Node.js中:

memcached.get("item" , function(err, data) {
  console.log(data);
}

你有什么办法吗?

推荐答案

arr = [...buffer]

ES6除缓冲区外还引入了许多其他功能.

ES6 introduced a lot of other features, besides buffers.

您甚至可以像这样轻松附加:

You can even easily append like this:

arr.push(...buffer)

...运算符可在数组中使用时扩展枚举,例如数组和缓冲区.还将它们扩展为单独的函数参数.

The ... operator expands enumerables such as arrays and buffers when used in array. It also expands them into separate function arguments.

是的,它也更快:

... :x100000: 835.850ms

... : x100000: 835.850ms

从原型进行切片调用:x100000: 2118.513ms

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);

这篇关于将缓冲区转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆