node.js 将缓冲区转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18148827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert buffer to array
提问by Ata
I am setting memcachedwith
我设置memcached与
$memcached->set("item" , ["1" => "hello"]);
anything work in PHP ,
任何在 PHP 中工作的东西,
In Node.js with memcachedplugin , I get a buffer instead of array in result
在带有memcached插件的Node.js 中,我得到了一个缓冲区而不是数组
<Buffer 61 3a 25 61 34 3a>
I can not convert such buffer to array
我无法将此类缓冲区转换为数组
In Node.js :
在 Node.js 中:
memcached.get("item" , function(err, data) {
console.log(data);
}
Do you have any way ?
你有什么办法吗?
回答by
arr = [...buffer]
arr = [...buffer]
ES6 introduced a lot of other features, besides buffers.
除了缓冲区之外,ES6 还引入了许多其他功能。
You can even easily append like this:
您甚至可以像这样轻松附加:
arr.push(...buffer)
arr.push(...buffer)
The ...operator expands enumerables such as arrays and buffers when used in array. It also expands them into separate function arguments.
...当在数组中使用时,运算符会扩展诸如数组和缓冲区之类的可枚举项。它还将它们扩展为单独的函数参数。
Yes, it's also faster:
是的,它也更快:
...: x100000: 835.850ms
Slice call from prototype: x100000: 2118.513ms
...:x100000:835.850ms
来自原型的切片调用: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);
回答by Chipintoza
Here you go:
干得好:
var buffer = new Buffer([1,2,3])
var arr = Array.prototype.slice.call(buffer, 0)
console.log(arr)
回答by DarckBlezzer
There is another way to convert to array of integers
还有另一种方法可以转换为整数数组
Using toJSON()
使用toJSON()
Buffer.from('Text of example').toJSON()
{ type: 'Buffer',data: [ 84, 101, 120, 116, 32, 111, 102, 32, 101, 120, 97, 109, 112, 108, 101 ] }
// simple get data
Buffer.from('Text of example').toJSON().data
[ 84, 101, 120, 116, 32, 111, 102, 32, 101, 120, 97, 109, 112, 108, 101 ]
Example of benchmark
基准示例
// I took this from @user4584267's answer
const 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]);
let array = null;
const iterations = 100000;
console.time("...buffer");
for (let i = iterations; i=i-1;) array = [...buffer]
console.timeEnd("...buffer");
console.time("array.prototype.slice.call");
for (let i = iterations; i=i-1;) array = Array.prototype.slice.call(buffer, 0)
console.timeEnd("array.prototype.slice.call");
console.time("toJSON().data");
for (let i = iterations; i=i-1;) array = buffer.toJSON().data
console.timeEnd("toJSON().data");
OUTPUT
输出
...buffer: 559.932ms
array.prototype.slice.call: 1176.535ms
toJSON().data: 30.571ms
...缓冲:559.932ms
array.prototype.slice.call:1176.535ms
的toJSON()数据:30.571ms
or if you want more profesional and custom function in Buffer use this:
或者,如果您想在 Buffer 中使用更多专业和自定义功能,请使用:
Buffer.prototype.toArrayInteger = function(){
if (this.length > 0) {
const data = new Array(this.length);
for (let i = 0; i < this.length; i=i+1)
data[i] = this[i];
return data;
}
return [];
}
Example of benchmark:
基准示例:
const 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]);
let array = null;
const iterations = 100000;
console.time("toArrayInteger");
for (let i = iterations; i=i-1;) buffer.toArrayInteger();
console.timeEnd("toArrayInteger");
Ouput:
输出:
toArrayInteger: 28.714ms
toArrayInteger:28.714ms
Note:In the last example I copied a function from Buffer.toJSONand custom it a lite
注意:在上一个示例中,我从Buffer.toJSON复制了一个函数并将其自定义为 lite
回答by Plato
I haven't used memcached so I am not sure just what this buffer represents or what you want to have instead. Sorry. Here is a function to split a buffer up into an array of bytes. More at node.js Buffer docs, hope it helps!
我没有使用过 memcached,所以我不确定这个缓冲区代表什么或者你想要什么。对不起。这是一个将缓冲区拆分为字节数组的函数。更多在node.js Buffer docs,希望它有帮助!
var hex = new Buffer("613a2561343a", "hex");
var l = hex.length; // in bytes
var output = [];
for(var i = 0; i < l; i++){
var char = hex.toString('hex',i,i+1); // i is byte index of hex
output.push(char);
};
console.log(output);
// output: [ '61', '3a', '25', '61', '34', '3a' ]
回答by hippietrail
You can also use Array.from:
您还可以使用Array.from:
memcached.get("item" , function(err, data) {
console.log(Array.from(data));
}
回答by JacobTDC
I have a solution, although I am currently trying to find a better one:
我有一个解决方案,尽管我目前正在努力寻找更好的解决方案:
function bufToArray(buffer) {
let array = new Array();
for (data of buffer.values()) array.push(data);
return array;
}
EDIT :I found a simpler way:
编辑:我找到了一种更简单的方法:
var buffer = Buffer.from('NodeJS rocks!')
var array = new Function(`return [${Array.prototype.slice.call(buffer, 0)}]`)
But, like someone already said, [...buffer]is faster (and more code efficient).
但是,就像有人已经说过的那样,[...buffer]速度更快(并且代码效率更高)。
You can also use new Uint8Array(buffer [, byteOffset [, length]]);
你也可以使用 new Uint8Array(buffer [, byteOffset [, length]]);
回答by Ata
In interent , there was no information about that , but I have found the convert way
实际上,没有关于此的信息,但我找到了转换方式
In nodejs , I have to use :
在 nodejs 中,我必须使用:
var arrayobject = phpjs.unserialize(data.toString());
but , it is very stupid way for getting array , it seem that php serilzie the data when setting memcache .
但是,这是一种非常愚蠢的获取数组的方式,似乎在设置内存缓存时 php serilzie 数据。

