facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.92k stars 642 forks source link

Performance Issue with hermes when we use crypto-js Library #735

Open tusharmutreja200 opened 2 years ago

tusharmutreja200 commented 2 years ago

Bug Description

We are using the crypto-js library to perform encryption and decryption tasks in our react-native app. After enabling Hermes in the Android application these tasks are taking 5x time as compared to Hermes was disable

Hermes version: 0.9.0 React Native version (if any):0.66.4 OS version (if any): Android 11 Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64):

Steps To Reproduce

code example:

Initially, the below code takes 1.5 sec to perform the task, but after enabling the Hermes it takes 6 sec to perform

const keyInside = CryptoJS.enc.Base64.parse(encryptionKey.value); const encryptedResult = CryptoJS.AES.encrypt(json, keyInside, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })

The Expected Behavior

It should perform as efficiently as it was working before enabling hermes

neildhar commented 2 years ago

@tusharmutreja200 this is similar to https://github.com/facebook/hermes/issues/626 and https://github.com/facebook/hermes/issues/569.

It is expected that JSC would outperform Hermes on some computationally intensive workloads on Android, since it has a JIT. In general, we would recommend offloading such operations to native code.

To confirm, are you running the app in release mode in both cases?

tusharmutreja200 commented 2 years ago

@neildhar yes I have tested the app in release mode. We will try to fix this via native code, but I really need to understand that do Hermes has any plan to fix this bug in a future release.

neildhar commented 2 years ago

There is currently no plan to write a JIT for Hermes. If you're able to share a simple repro of the performance difference we could look into whether it is hitting performance bottlenecks in the interpreter.

parth3724 commented 2 years ago

Facing similar issue for AES decrypt

import Logger from '../../Log/Logger';
import { decode as atob } from 'base-64';
var CryptoJS = require('crypto-js');

exports.decryptAES = (key, cryptText, iv): string => {
  try {
    let plainText = CryptoJS.AES.decrypt(
      { ciphertext: CryptoJS.enc.Latin1.parse(cryptText) },
      CryptoJS.enc.Base64.parse(key),
      { iv: CryptoJS.enc.Latin1.parse(iv) }
    );

    return plainText.toString(CryptoJS.enc.Utf8);
  } catch (err) {
    Logger.e('decryptAES', 'error while decrypting', err);
    throw err;
  }
};

Latency of decryptAES for same parameters on same device in release app- without hermes - ~1000 ms. with hermes - ~15000 ms.