Does JavaScript store reference to every element in array of ints? - javascript

In C\C++, Java, C# code like this
arr = new int[5];
will be stored in memory using 5 * 4 = 40 bytes, since every int just a 32 bits number.
Is it true for JavaScript? Or does it store ints as references to the objects that contain boxed ints?

Related

Achieve new BigInteger(130, new SecureRandom()).toString(32) in JavaScript

I understand that we can use window.crypto to generate a secure random number, however window.crypto.getRandomValues() is using typedArray.
May I know how can we achieve exactly this Java function in JavaScript:
new BigInteger(130, new SecureRandom()).toString(32)
You could generate four 32-bit numbers for a total of 128 - close to your Java maximum of 130 (the value specified to the constructor of BigInteger is the maximum number of bits, as stated in the docs), then convert them all to base 32, and finally join them together.
function getSecureRandom() {
// allocate space for four 32-bit numbers
const randoms = new Uint32Array(4);
// get random values
window.crypto.getRandomValues(randoms);
// convert each number to string in base 32, then join together
return Array.from(randoms).map(elem => elem.toString(32)).join("");
}
The Array#from call is necessary because TypedArray.prototype.map returns another TypedArray, and a typed array cannot contain strings. We first convert the typed array randoms into a normal array, then call .map() on it.

How to cast an `ArrayBuffer` to a `SharedArrayBuffer` in Javascript?

Take the following snippet:
const arr = [1.1, 2.2, 3.3]
const arrBuffer = (Float32Array.from(arr)).buffer
How would one cast this ArrayBuffer to a SharedArrayBuffer?
const sharedArrBuffer = ...?
Note that both ArrayBuffer and SharedArrayBuffer are backing data pointers that you only interact with through a typed array (like Float32Array, in your example). Array Buffers represent memory allocations and can't be "cast" (only represented with a typed array).
If you have one typed array already, and need to copy it into a new SharedArrayBuffer, you can do that with set:
// Create a shared float array big enough for 256 floats
let sharedFloats = new Float32Array(new SharedArrayBuffer(1024));
// Copy floats from existing array into this buffer
// existingArray can be a typed array or plain javascript array
sharedFloats.set(existingArray, 0);
(In general, you can have a single array buffer and interact with it through multiple "typed lenses" - so, basically, casting an array buffer into different types, like Float32 and Uint8. But you can't cast an ArrayBuffer to a SharedArrayBuffer, you'll need to copy its contents.)

How to convert a Buffer to an array of ints node.js

I have a c# application that converts a double array to a byte array of data to a node.js server which is converted to a Buffer (as convention seems to recommend). I want to convert this buffer into an array of the numbers originally stored in the double array, I've had a look at other questions but they either aren't applicable or just don't work ([...buf], Array.prototype.slice.call(buf, 0) etc.).
Essentially I have a var buf which contains the data, I want this to be an array of integers, is there any way I can do this?
Thank you.
First, you need to know WHAT numbers are in the array. I'll assume they are 32bit integers. So first, create encapsulating Typed Array around the buffer:
// #type {ArrayBuffer}
var myBuffer = // get the bufffer from C#
// Interprets byte array as 32 bit int array
var myTypedArray = new Int32Array(myBuffer);
// And if you really want standard JS array:
var normalArray = [];
// Push all numbers from buffer to Array
normalArray.push.apply(normalArray, myTypedArray);
Note that stuff might get more complicated if the C#'s array is in Big Endian, but I assume it's not. According to this answer, you should be fine.
I managed to do this with a DataView and used that to iterate over the buffer, something I'd tried before but for some reason didn't work but does now.

How to use Javascript DataView and ArrayBuffer to set byte-sized MODBUS data correctly for multi-byte data types

I am using jsmodbus npm library to retrieve 16 bit registers using readHoldingRegisters() function.
// array of values returned by jsmodbus readHoldingRegisters for two 16 bit registers
data = [ 17008, 28672 ]
I am using an ArrayBuffer and DataView to set and get the data in the required format:
const buffer = new ArrayBuffer(4)
const view = new DataView(buffer)
I understand that data returned from registers is always 16-bit integers split over two 8-bit byte values, so should I set the two bytes to the view as two consecutive ints even though it may be later retrieved from the view as an Int16 or Float32. Is this correct?
Secondly, if I expect to retrieve the data as signed Int16 or Float32, is it necessary to set the high byte as signed and the low byte as unsigned, like so:
view.setInt16(0, data[0])
view.setUint16(0, data[1])
And third: notwithstanding the need to set proper endianness, does it even matter which method you use when setting the data against the view, as the order of bytes and bits in the view isn't affected by which method you set against, only when you retrieve that data back out?
Certainly, it doesn't appear to:
view.setInt16(0, data[0])
view.setInt16(1, data[1]) // notice: setInt16
val = view.getFloat32(0)
// val = 122.880859375
view.setInt16(0, data[0])
view.setUint16(1, data[1]) // notice: set*U*int16
val = view.getFloat32(0)
// val = 122.880859375
Sanity check appreciated!

What is the use of type javascript/javascriptwithscope of bson

I am wondering about the use of these two type of bson (javascript/javascriptwithscope);
as the base type of bson.
What's the use case of it and how to generate a javascriptwithscope object to save in mongodb?
Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer”
JavaScript 13 “javascript”
Symbol 14 “symbol”
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Min key -1 “minKey”
Max key 127 “maxKey”
Basically we need to do nothing :-) with data type as mongo engine will apply correct type to inserted data.
When creating a javascript object which will be inserted to mongo:
var object = {
thisWillBeNumber : 1,
thisWillBeString :"aaa",
thisWillBeAnArray = [1,2,3]
thisWillBeDateTime: new Date()
}
then mongo uses javascript object type and savs it.
In some drivers/framework we can enforce on application level types of our field/variables and such information could be added to stored document.

Categories