MV How can I optimize this code?

I've been transitioning my code from node.js modules to web apis to try and port it to bowser and mobile, but I'm running into some performance issues.

The following code is for encrypting and decrypting text. Originally, it would take 1-2ms to do so using the crypto module, but using the webcrypto api each now takes about 30-60ms, which added on top of ping makes it a big problem. Does anybody know what I can do to further improve performance?

JavaScript:
const textEncoder = new TextEncoder(); // Reuse encoder for performance

async function encrypt(text) {
  if (!decodepass) return;

  const textBytes = textEncoder.encode(text);

  if (!keye) {
    keye = await crypto.subtle.importKey(
      'raw',
      decodepass,
      { name: 'AES-CBC' },
      false,
      ['encrypt']
    );
  }

  try {
    const encryptedBuffer = await crypto.subtle.encrypt(
      { name: 'AES-CBC', iv: decodeiv },
      keye,
      textBytes
    );

    const encryptedArray = new Uint8Array(encryptedBuffer);
    let result = '';
    for (let i = 0; i < encryptedArray.length; i += 0x8000) {
      result += String.fromCharCode.apply(null, encryptedArray.subarray(i, i + 0x8000));
    }
    return result;
  } catch (e) {
    return null; // Return null on failure
  }
}

 
  const textDecoder = new TextDecoder('utf-8'); // Reuse decoder for performance

  async function decrypt(text) {
    if (!keyd) {
      keyd = await crypto.subtle.importKey(
        'raw',
        decodepass,
        { name: 'AES-CBC' },
        false,
        ['decrypt']
      );
    }
 
    try {
      const encryptedData = Uint8Array.from(text, c => c.charCodeAt(0));
      const decryptedBuffer = await crypto.subtle.decrypt(
        { name: 'AES-CBC', iv: decodeiv },
        keyd,
        encryptedData
      );
      return textDecoder.decode(decryptedBuffer);
    } catch (e) {
      return text; // fallback on error
    }
  }
It is a hard to tell the differences. If you are sure it is web crypto's problem, is it possible to store keyd and keye instead of import them every time?
It is a hard to tell the differences. If you are sure it is web crypto's problem, is it possible to store keyd and keye instead of import them every time?
according to my performance testing they seem to be. Also the keyd and keye *are* being stored, as you can see in the code
Oh, I see.
Well during my test, I notice there seems to be a 'warm up' time when you call encrypt.
For example, a long text takes 60ms to finish for the first time after I boot up my computer. But when I try another long text, it is 2ms or so.

So maybe you can let it encrypt some dummy messages first when the game is booting.

By the way, I'm using the newest chrome browser so maybe it is a bit different.
Oh, I see.
Well during my test, I notice there seems to be a 'warm up' time when you call encrypt.
For example, a long text takes 60ms to finish for the first time after I boot up my computer. But when I try another long text, it is 2ms or so.

So maybe you can let it encrypt some dummy messages first when the game is booting.

By the way, I'm using the newest chrome browser so maybe it is a bit different.
you ran exactly that code and it took 2ms? Hm, maybe it is a bug with the version of nwjs I'm using....
Actually you can try the API demo here: https://mdn.github.io/dom-examples/web-crypto/encrypt-decrypt/index.html

You can save the page and run it using nw.js
The only difference is importKey but if the key is cached it should only affect the first run.

So maybe your encrypted message is very long?

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-optimize-this-code.177109/