aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/js-perf-test/StackTrace/serialize.js
blob: bbac87647cf1a6107f649eb6159ce06e94f623e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function() {

const kErrorCount = 100000;
let errorsCreatedBySetup;

function CreateErrors(fn) {
  counter = 0;
  errorsCreatedBySetup = [];
  for (let i = 0; i < kErrorCount; ++i) {
    errorsCreatedBySetup[i] = fn();
  }
}

function SimpleSetup() {
  CreateErrors(() => new Error("Simple Error"));
}

class CustomError extends Error {};
function CustomSetup() {
  CreateErrors(() => new CustomError("Custom Error"));
}

function InlineSetup() {
  function Inner() {
    return new Error("Throwing from inlined function!");
  }
  function Middle() { return Inner(); }
  function Outer() { return Middle(); }

  Outer();
  Outer();
  %OptimizeFunctionOnNextCall(Outer);
  Outer();

  CreateErrors(() => Outer());
}

const kInitialRecursionValue = 12;
function RecursiveSetup() {
  counter = 0;
  errorsCreatedBySetup = [];
  function StepOne(val) {
    if (val <= 0) {
      errorsCreatedBySetup.push(new Error("Error in StepOne!"));
      return;
    }
    StepTwo(val - 3);
    StepTwo(val - 4);
  }
  function StepTwo(val) {
    if (val <= 0) {
      errorsCreatedBySetup.push(new Error("Error in StepTwo!"));
      return;
    }
    StepOne(val - 1);
    StepOne(val - 2);
  }

  while (errorsCreatedBySetup.length < kErrorCount) {
    StepOne(kInitialRecursionValue);
  }
}

let counter;
function SerializeStack() {
  if (counter < errorsCreatedBySetup.length) {
    // Trigger serialization by accessing Error.stack.
    %FlattenString(errorsCreatedBySetup[counter++].stack);
  } else {
    // All errors are serialized. The stack trace string is now cached, so
    // re-iterating the array is a simple property lookup. Instead,
    // a simple Error object is created and serialized, otherwise the benchmark
    // result would fluctuate heavily if it reaches the end.
    %FlattenString(new Error().stack);
  }
}

createSuite('Simple-Serialize-Error.stack', 1000, SerializeStack, SimpleSetup);
createSuite('Custom-Serialize-Error.stack', 1000, SerializeStack, CustomSetup);

createSuite('Inline-Serialize-Error.stack', 1000, SerializeStack, InlineSetup);
createSuite('Recursive-Serialize-Error.stack', 1000, SerializeStack, RecursiveSetup);

})();