传统的JavaScript代码库移植到OpenHarmony上,性能会受影响?提升性能的优化建议? 精华

csdn-qq8864 显示全部楼层 发表于 2024-2-28 16:54:04

发生了什么问题?

笔者最近移植了npm仓库下的三方开源库jsbn (大数计算库)和sm-cripto库(sm2国密算法库)。发布到了OpenHarmony三方库中心仓。

但用户反馈sm2加密算法很慢,得需要十多秒(在api9的P50模拟器中的测试结果)。所以想问下传统的JavaScript代码库移植到鸿蒙系统上,性能是否受影响? 提升性能的优化建议?同样的代码测试用例,在nodejs下的运行时间仅28ms. 运行时间相差悬殊。

期望行为是什么?

传统的JavaScript代码库移植到鸿蒙系统上,性能是否受到影响? js的代码运行会不会很慢?提升性能的优化建议什么?针对第三方js库呢?

希望至少让移植的算法库,效率上打到能用的状态。一个加密操作耗时10多秒,无法接受啊。难道是在模拟器运行的原因?不该差别这么悬殊。

如何复现该缺陷

测试用例:

windows系统的nodejs下(耗时28ms):

const sm2 = require('sm-crypto').sm2

const keypair = sm2.generateKeyPairHex();

const publicKey = keypair.publicKey;
const privateKey = keypair.privateKey;
console.info(publicKey);
console.info(privateKey);
        // encrypt & decrypt

const message = "Hello World!";
let startTime = Date.now();
const encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result

let endTime = Date.now();
let executionTime = endTime - startTime;

console.log(`myFunction executed in ${executionTime} ms`);

在鸿蒙上的测试代码(在模拟器P50中运行,耗时18s):

import { sm2,sm3,sm4 } from '@yyz116/sm-crypto'

const keypair = sm2.generateKeyPairHex();

const publicKey = keypair.publicKey;
const privateKey = keypair.privateKey;
console.info(publicKey);
console.info(privateKey);
        // encrypt & decrypt

const message = "Hello World!";
let startTime = Date.now();
const encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result

let endTime = Date.now();
let executionTime = endTime - startTime;

console.log(`myFunction executed in ${executionTime} ms`);

其他补充信息

鸿蒙 测试环境: Dev-Studio4.0 , harmonyos-sdk-API -9 ,P50模拟器中运行

电脑上测试环境: win10-x64 cpu(Core(TM) i5-11400 @ 2.60GHz), nodejsv18.13.0

输入图片说明

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论6

马迪

沙发 发表于 2024-2-29 17:26:54
1 模拟器上现在集成的是C 解释器,效率确实会受影响。
2 对与密集计算的JS 代码,要想拿到高性能需要JIT等技术来优化,
目前鸿蒙默认的JS引擎还没有开启JIT,会在后续版本开启

当前可能需要改成arkts才行

bananaCaptain

板凳 发表于 2024-6-12 11:16:50
请问JavaScript代码库 移植为 har包有相关教程不?请问哪里可以学习相关内容。谢谢

马迪

发表于 2024-6-12 13:37  IP属地: 贵州省贵阳市

回复 bananaCaptain: https://ohpm.openharmony.cn/#/cn/help/publishrequirefile   这个?

bananaCaptain

发表于 2024-6-13 10:12  IP属地: - 中国广东省广州市 中国电信IDC

回复 马迪: 我在使用js-e2e 扫描想移植的库时出现了'window' is not defined 知道怎么调整不?麻烦了

D:\NodeProjects\js-e2e\js-compatibiitiy\temp\node_modules\leaflet\dist\leaflet-src.esm.js
    210:9   error  'window' is not defined             no-undef
    210:36  error  'window' is not defined             no-undef
    210:60  error  'window' is not defined             no-undef
    221:9   error  'window' is not defined             no-undef
    224:17  error  'window' is not defined             no-undef
    225:16  error  'window' is not defined             no-undef
    226:65  error  'window' is not defined             no-undef
    238:25  error  'window' is not defined             no-undef
    246:17  error  'window' is not defined             no-undef
   1888:9   error  'document' is not defined           no-undef
   1929:13  error  'document' is not defined           no-undef
   1932:29  error  'window' is not defined             no-undef
   1935:20  error  'document' is not defined           no-undef
   1938:29  error  'navigator' is not defined          no-undef
   1938:62  error  'document' is not defined           no-undef
   1952:52  error  'navigator' is not defined          no-undef
   1954:98  error  'window' is not defined             no-undef
   1957:15  error  'window' is not defined             no-undef
   1975:11  error  'navigator' is not defined          no-undef
   1981:38  error  'window' is not defined             no-undef

马迪

发表于 2024-6-13 11:24  IP属地: - 中国 中国电信股份有限公司云计算分公司

回复 bananaCaptain: 要看代码逻辑了,鸿蒙里没有全局window对象,需要人工解决掉

bananaCaptain

发表于 2024-6-14 13:46  IP属地: - 中国广东省广州市 中国电信IDC

回复 马迪: 有没有解决过window,navigator,document的har库案例推荐参考一下如何修改好。感谢

【4 条回复】

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部