-1

我有 3 个打字稿算法需要在浏览器中进行基准测试。我试过 area51,area51 只适用于 JavaScript。我还发现了一个pastebin:

//pastebin.com/gCs9CB5F

有人可以给我一个提示如何在 TypeScript 上运行基准测试吗?

4

2 回答 2

0

使用以下过程:

例如,这个 TypeScript 代码:

function add(x: number, y: number): number {
    return x + y;
}

console.log(add(2,2)); // 4

转译后可以进行基准测试,如下所示:

var bench1 = new Benchmark(
  {'fn':add, 'cycles':0,'count':1,'name':'bench1','async':true}
);

function add(x, y) {
    return x + y;
}

function log(result){console.log(JSON.parse(JSON.stringify(result.target.stats)))};

bench1.on('complete', log);
bench1.run();
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.2/benchmark.min.js"></script>

参考

于 2017-07-20T19:30:39.670 回答
0

JavaScript 是 typescript,所以开始用 typescript 编写你的性能测试吧。我能想到的唯一可能会影响性能的打字稿是 target您在 tsconfig.json 中使用哪个 - 由于 pollyfills,es5 可能会比 es2018 慢。js标准中不存在的语言功能也很有趣,例如https://www.typescriptlang.org/docs/handbook/decorators.html

于 2018-10-25T03:21:39.120 回答