mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
perf: faster node globals access in cjs (#19997)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
d5efdeeff1
commit
78ceeec6be
2 changed files with 35 additions and 3 deletions
|
@ -1,4 +1,3 @@
|
|||
// package that has all the locals defined
|
||||
const Buffer = 1, clearImmediate = 1, clearInterval = 1, clearTimeout = 1, console = 1, global = 1, process = 1, setImmediate = 1, setInterval = 1, setTimeout = 1, globalThis = 1;
|
||||
const exports = 2;
|
||||
require("./other.js");
|
||||
|
|
|
@ -40,6 +40,8 @@ const {
|
|||
Error,
|
||||
TypeError,
|
||||
} = primordials;
|
||||
import { nodeGlobals } from "ext:deno_node/00_globals.js";
|
||||
|
||||
import _httpAgent from "ext:deno_node/_http_agent.mjs";
|
||||
import _httpOutgoing from "ext:deno_node/_http_outgoing.ts";
|
||||
import _streamDuplex from "ext:deno_node/internal/streams/duplex.mjs";
|
||||
|
@ -915,9 +917,15 @@ Module.prototype.require = function (id) {
|
|||
}
|
||||
};
|
||||
|
||||
// The module wrapper looks slightly different to Node. Instead of using one
|
||||
// wrapper function, we use two. The first one exists to performance optimize
|
||||
// access to magic node globals, like `Buffer` or `process`. The second one
|
||||
// is the actual wrapper function we run the users code in.
|
||||
// The only observable difference is that in Deno `arguments.callee` is not
|
||||
// null.
|
||||
Module.wrapper = [
|
||||
"(function (exports, require, module, __filename, __dirname) { (function () {",
|
||||
"\n}).call(this); })",
|
||||
"(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, console, global, process, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {",
|
||||
"\n}).call(this, exports, require, module, __filename, __dirname); })",
|
||||
];
|
||||
Module.wrap = function (script) {
|
||||
script = script.replace(/^#!.*?\n/, "");
|
||||
|
@ -982,6 +990,20 @@ Module.prototype._compile = function (content, filename) {
|
|||
ops.op_require_break_on_next_statement();
|
||||
}
|
||||
|
||||
const {
|
||||
Buffer,
|
||||
clearImmediate,
|
||||
clearInterval,
|
||||
clearTimeout,
|
||||
console,
|
||||
global,
|
||||
process,
|
||||
setImmediate,
|
||||
setInterval,
|
||||
setTimeout,
|
||||
performance,
|
||||
} = nodeGlobals;
|
||||
|
||||
const result = compiledWrapper.call(
|
||||
thisValue,
|
||||
exports,
|
||||
|
@ -989,6 +1011,17 @@ Module.prototype._compile = function (content, filename) {
|
|||
this,
|
||||
filename,
|
||||
dirname,
|
||||
Buffer,
|
||||
clearImmediate,
|
||||
clearInterval,
|
||||
clearTimeout,
|
||||
console,
|
||||
global,
|
||||
process,
|
||||
setImmediate,
|
||||
setInterval,
|
||||
setTimeout,
|
||||
performance,
|
||||
);
|
||||
if (requireDepth === 0) {
|
||||
statCache = null;
|
||||
|
|
Loading…
Reference in a new issue