Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow when looping thrugh a big array #13

Open
Sivaranjith1 opened this issue Jun 22, 2020 · 2 comments
Open

Slow when looping thrugh a big array #13

Sivaranjith1 opened this issue Jun 22, 2020 · 2 comments

Comments

@Sivaranjith1
Copy link

I am trying to create an app which runs in real time, but when I test it the array looping is so slow it is not usable

const ffi = require('ffi-napi');
const ref = require('ref-napi');
const ArrayType = require('ref-array-di')(ref);

const int = ref.types.int;
const IntArray = ArrayType(int);

let arr = new IntArray(1280*1080*3)

console.time('time')
for (let i = 0; i < arr.length; i++) {
		const element = arr[i];
	}
console.timeEnd("time")

This takes 3394ms to loop through. Do anyone know how to fix this?

@addaleax
Copy link
Contributor

Adding native support for TypedArrays would probably make the most sense here, but this library was written before those were commonplace. I probably won’t have the time to do so, since I’m maintaining this as a side project.

@AntonK93
Copy link

AntonK93 commented Mar 17, 2023

I also faced an issue with ref-array-di which was expressed in the slowness operation time of application, in my case it was in case of array initialization, took about 10 seconds to allocate RAM for the array size of 6912000 bytes.
Cause of that i started using js native Buffer.alloc() and pass its buffer into lib function, in your case that can be replaced as well.

so you can measure time for the next one

const buffer = Buffer.alloc(1280*1080*3);

console.time('time')
for (let [index, byte] of buffer.entries()) {
  const element = buffer[index];
}
console.timeEnd("time")

and an example of using buffer as an array in lib interactions

import ref from 'ref-napi';
import { Library } from 'ffi-napi';

const lib = new Library('lib', {
        funcToCall: ['int', [ref.refType('uchar *')]],
    });
const arrayBuffer = Buffer.alloc(6912000); // i know specific size of array from the lib sources so such allocated memory is enough in my case

lib.funcToCall(arrayBuffer);
for (let [index, byte] of arrayBuffer.entries()) { // here you get filled buffer with values from your library and you can continue processing with as you need to
  const element = buffer[index];
}

All of this may not be very useful because it's a workaround but since i was struggled with fixing the slowness during array creation there might be someone who will face the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants