"hello"]); something works in php In nodejs with a memc...">

Convert buffer to array

I install memcached using

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

something works in php

In nodejs with a memcached plugin, I get a buffer instead of an array as a result

 <Buffer 61 3a 25 61 34 3a> 

I cannot convert such a buffer to an array

in nodjs:

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

Do you have any way?

+8
javascript memcached
source share
5 answers

arr = [...buffer]

ES6 introduced many other features besides buffers.

You can simply add the following:

arr.push(...buffer)

The ... operator expands subnets such as arrays and buffers when used in an array. He also extends them to separate function arguments.


Yes, it is also faster:

... : x100000: 835.850ms

Calling a fragment from a 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); 
+14
source share

Here you go:

 var buffer = new Buffer([1,2,3]) var arr = Array.prototype.slice.call(buffer, 0) console.log(arr) 
+5
source share

I have not used memcached, so I'm not sure what exactly represents this buffer or what you want to use. I'm sorry. Here is the function to split the buffer into an array of bytes. More details in node.js Buffer documents , hope this helps!

 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' ] 
+1
source share

There was no information about this in the interval, but I found a way to convert

In nodejs, I should use:

 var arrayobject = phpjs.unserialize(data.toString()); 

but, this is a very stupid way to get an array, it seems that php serilzie data when installing memcache.

-one
source share

I have a solution, although I'm currently trying to find a better option:

 function bufToArray(buffer) { let array = new Array(); for (data of buffer.values()) array.push(data); return array; } 

EDIT: I found an easier way:

 var buffer = Buffer.from('NodeJS rocks!') var array = new Function(`return [${Array.prototype.slice.call(buffer, 0)}]`) 

But, as someone already said, [...buffer] is faster (and more efficient than code).

You can also use new Uint8Array(buffer [, byteOffset [, length]]);

-one
source share

All Articles