1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

perf: faster node globals access in cjs (#19997)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Luca Casonato 2023-07-31 21:45:32 +02:00 committed by GitHub
parent d5efdeeff1
commit 78ceeec6be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 3 deletions

View file

@ -1,4 +1,3 @@
// package that has all the locals defined // 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 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"); require("./other.js");

View file

@ -40,6 +40,8 @@ const {
Error, Error,
TypeError, TypeError,
} = primordials; } = primordials;
import { nodeGlobals } from "ext:deno_node/00_globals.js";
import _httpAgent from "ext:deno_node/_http_agent.mjs"; import _httpAgent from "ext:deno_node/_http_agent.mjs";
import _httpOutgoing from "ext:deno_node/_http_outgoing.ts"; import _httpOutgoing from "ext:deno_node/_http_outgoing.ts";
import _streamDuplex from "ext:deno_node/internal/streams/duplex.mjs"; 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 = [ Module.wrapper = [
"(function (exports, require, module, __filename, __dirname) { (function () {", "(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); })", "\n}).call(this, exports, require, module, __filename, __dirname); })",
]; ];
Module.wrap = function (script) { Module.wrap = function (script) {
script = script.replace(/^#!.*?\n/, ""); script = script.replace(/^#!.*?\n/, "");
@ -982,6 +990,20 @@ Module.prototype._compile = function (content, filename) {
ops.op_require_break_on_next_statement(); ops.op_require_break_on_next_statement();
} }
const {
Buffer,
clearImmediate,
clearInterval,
clearTimeout,
console,
global,
process,
setImmediate,
setInterval,
setTimeout,
performance,
} = nodeGlobals;
const result = compiledWrapper.call( const result = compiledWrapper.call(
thisValue, thisValue,
exports, exports,
@ -989,6 +1011,17 @@ Module.prototype._compile = function (content, filename) {
this, this,
filename, filename,
dirname, dirname,
Buffer,
clearImmediate,
clearInterval,
clearTimeout,
console,
global,
process,
setImmediate,
setInterval,
setTimeout,
performance,
); );
if (requireDepth === 0) { if (requireDepth === 0) {
statCache = null; statCache = null;