• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16class Context {
17  compute(exec: () => void) {
18    totalCompute++
19    if (true) {
20      exec()
21    }
22  }
23  computeNoLambda() {
24    totalCompute++
25    return true
26  }
27}
28const ctx = new Context()
29let totalWorkload = 0
30let totalCompute = 0
31
32function benchLambda() {
33  ctx.compute(() => {
34    totalWorkload++
35  })
36}
37function benchNoLambda() {
38  if (ctx.computeNoLambda()) {
39    totalWorkload++
40  }
41}
42
43let start: number = 0
44const N = 10000000
45
46function Benchmark() {
47  console.log("Starting benchmarks, N="+N)
48  totalWorkload = 0
49  totalCompute = 0
50  console.log("Starting lambda benchmark")
51  start = Date.now()
52  for (let i=0; i<N; i++) {
53    benchLambda()
54  }
55  console.log("lambda time: " + (Date.now() - start).toString())
56
57  totalWorkload = 0
58  totalCompute = 0
59  console.log("Starting no-lambda benchmark")
60  start = Date.now()
61  for (let i=0; i<N; i++) {
62    benchNoLambda()
63  }
64  console.log("no lambda time: " + (Date.now() - start).toString())
65}
66
67Benchmark()
68
69