Skip to content

First invoke of module's (crypto) method is extremely slow #2073

Closed

Description

I've noticed quite a big lag while adding first record in my application and after doing some tests it turned out that running crypto.randomBytes() for the first time takes incomparably longer than for all the consecutive runs. Here's the test code:

console.time('require');
var crypt = require('crypto');
console.timeEnd('require');

for (var i = 1; i < 4; i++) {
  console.time(i);
  crypt.randomBytes(10);
  console.timeEnd(i);
}

Running this with node produces:

require: 9ms
1: 0ms
2: 0ms
3: 0ms

and a simple electron app:

[988:0628/194243:INFO:CONSOLE(0)] require: 9ms
[988:0628/194243:INFO:CONSOLE(0)] 1: 525ms
[988:0628/194243:INFO:CONSOLE(0)] 2: 0ms
[988:0628/194243:INFO:CONSOLE(0)] 3: 0ms

and it's even longer if run within browser-window.

Activity

  1. zcbenz commented on Jun 29, 2015

    @zcbenz
    Contributor

    Which OS are you using?

  2. jrieken commented on Mar 7, 2017

    @jrieken

    We have observed the same in VS Code (microsoft/vscode#22086) with times of around 1 second in the renderer. Also, we pay the price every time electron starts, whereas node isn't slow on subsequent starts.

    Put the below snippet in renderer.js of the quick start sample, keep refreshing the renderer and see it always being slow, not (re-)using any sort of OS-level entropy

    let t1 = Date.now(); 
    console.log(require('crypto').randomBytes(21).toString('hex')); 
    console.log(`${Date.now() - t1}ms`);
  3. niik commented on Apr 27, 2017

    @niik
    Contributor

    We've also observed the same behavior with initial load times of over a second on Windows 10. We spotted it using the uuid module (see uuidjs/uuid#189 (comment))

    We've worked around it for now by using window.crypto as our source of entropy for uuid but since we could end up having one of our other dependencies source through the NodeJS crypto module it'd be real nice if we could solve this in electron proper.

  4. chgibb commented on Apr 27, 2017

    @chgibb

    As a temporary work around, we've switched to using node-uuid's v4 module as it does not invoke crypto.randomBytes at require time.

  5. Doccrazy commented on Nov 18, 2017

    @Doccrazy

    I was able to work around the issue by initializing the crypto module on application start using the callback of the crypto.randomBytes method. This way, the initial load is performed in the background, without blocking the UI thread. A later synchronous call can then be made without delay.

    setTimeout(() => {
      // asynchronously initialize the crypto library
      crypto.randomBytes(1, () => {
        // done
      });
    }, 1000);

    Still, a proper fix would be nice, esp. as the bug also applies to the RSA methods (crypto.privateDecrypt etc.).

  6. nornagon commented on Apr 30, 2019

    @nornagon
    Contributor

    I can't reproduce this on Electron 3, 4 or 5. Here's the fiddle I'm using: https://gist.github.com/9b2e03eb35953a8e47342c7d9a9b94be

    Is there something I'm missing? I tried this on Windows and macOS, and didn't observe any timings above ~20ms.

  7. zcbenz commented on Apr 30, 2019

    @zcbenz
    Contributor

    Probably silently fixed in upstream.

  8. nornagon commented on May 1, 2019

    @nornagon
    Contributor

    Closing this for now, reopen if you find a way to reproduce it :)

  9. PriyankuBiswas-EH commented on Oct 3, 2024

    @PriyankuBiswas-EH

    The issue is resolved in v32.1.2 as far i see. In version 26 it was very slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions