Skip to content

Slow Uint8Array comparison #446

@appy-one

Description

@appy-one

While creating unit tests for AceBase, I had to compare whether stored binary data equals the generated data, which is 5MB large. The expect comparison method is very slow, takes about 5 seconds:

// Generate data
let data = new Uint8Array(5 * 1000 * 1000);
for(let i = 0 ; i < data.length; i++) { data[i] = i % 255; }
// .. Store in the database & retrieve it again into variable 'stored'
expect(stored).toEqual(data); // <-- SLOW

If I compare the data myself with a loop, this is way faster (takes few ms):

let isEqual = true;
for(let i = 0; i < data.length && isEqual; i++) {
   isEqual = data[i] === stored[i]; 
}
expect(isEqual).toBeTrue();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions