Many records to JS TypedArray
See original GitHub issueHi. I need fetch many (100k+) rows from table, in row only one column type - int4.
const result = query('SELECT "int4_value" FROM generate_series(1, 100001) int4_value');
const typedArray = Int32Array(result.rows.length);
// This over used memory and utilization proc
result.rows.forEach((row, index) => {
typedArray[index] = row.int4_value;
});
I want a more effective solution. So as not to load data into the JS array, get the whole result as a ArrayBuffer
Something like this:
const result = query({
text: 'SELECT "int4_value" FROM generate_series(1, 100001) int4_value',
type: 'arrayBuffer',
});
const typedArray = Int32Array(result.arrayBuffer);
Is this possible?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (4 by maintainers)
Top Results From Across the Web
JavaScript typed arrays - MDN Web Docs
JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers.
Read more >20. Typed Arrays - Exploring JS
Instances of DataView let you access data as elements of several types ( Uint8 , Int16 , Float32 , etc.), at any byte...
Read more >Javascript TypedArray performance - Stack Overflow
Modern engines will use true arrays behind the scenes even if you use Array if they think they can, falling back on property...
Read more >Typed arrays - Binary data in the browser - web.dev
A Typed Array is a slab of memory with a typed view into it, much like how arrays work in C. Because a...
Read more >JavaScript Typed Array Reference - W3Schools
JavaScript Typed Arrays. In Javascript, a typed array is an array-like buffer of binary data. There is no JavaScript property or object named...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The Int32Array constructor treats each element in the Buffer as a value so you’ll get data.length elements (not 4-bytes at a time). I don’t think there’s a direct way to convert it without some extra module (would be really easy in C…). You can do it manually via:
This outputs:
Not sure how slow that’d be at scale though.
You’re better off allocating it once and reading the results via a cursor. That way it’ll scale out to any number of entries and node only needs to allocate memory for a fixed number of rows. Try something like this:
I’m getting about .5 seconds to run that for 250K entries:
You can play with NUM_ROWS and CHUNK_SIZE to see how it effects memory and speed.
.buffer
is the backingArrayBuffer
, and.byteOffset
and.length
are used correctly. It’s the right approach, just for the wrong endianness.Is the manual loop not better? There’s also the option of: