aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/module-exports-putter.cjs
blob: 9bef17b90e1a77765473d3e882675d0e561a3385 (plain) (blame)
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
// This is a stress test of some internals in How Bun does the module.exports assignment.
// If it crashes or throws then this fails
import("./runner.mjs").then(({ bench, run }) => {
  bench("Object.defineProperty(module, 'exports', { get() { return 42; } })", () => {
    Object.defineProperty(module, "exports", {
      get() {
        return 42;
      },
      set() {
        throw new Error("bad");
      },
      configurable: true,
    });
    if (module.exports !== 42) throw new Error("bad");
    if (!Object.getOwnPropertyDescriptor(module, "exports").get) throw new Error("bad");
  });

  bench("Object.defineProperty(module.exports = {})", () => {
    Object.defineProperty(module, "exports", {
      value: { abc: 123 },
    });

    if (!module.exports.abc) throw new Error("bad");
    if (Object.getOwnPropertyDescriptor(module, "exports").value !== module.exports) throw new Error("bad");
  });

  bench("module.exports = {}", () => {
    module.exports = { abc: 123 };

    if (!module.exports.abc) throw new Error("bad");
    if (Object.getOwnPropertyDescriptor(module, "exports").value !== module.exports) throw new Error("bad");
  });

  run().then(() => {
    module.exports = {
      a: 1,
    };

    console.log(
      module?.exports,
      require.cache[module.id].exports,
      module?.exports === require.cache[module.id],
      __dirname,
      Object.keys(require(module.id)),
      require(module.id),
    );

    module.exports = function lol() {
      return 42;
    };

    console.log(module.exports, module.exports());

    queueMicrotask(() => {
      console.log(
        module?.exports,
        require.cache[module.id].exports,
        module?.exports === require.cache[module.id]?.exports,
        __dirname,
        Object.keys(require(module.id)),
        require(module.id),
      );
    });
  });
});