GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lib: flag to conditionally modify proto on deprecate · nodejs/node@d555db2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/util
    • deprecate.js
  • lib/internal
    • util.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/util/deprecate.js‎

Copy file name to clipboard
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e5],
8+
modifyPrototype: [1, 0],
9+
emitWarningSync: [1, 0],
10+
}, {
11+
flags: ['--expose-internals'],
12+
});
13+
14+
function simpleFunction(x) {
15+
return x * 2 + (new Array(1000)).fill(0).map((_, i) => i).reduce((a, b) => a + b, 0);
16+
}
17+
18+
function main({ n, modifyPrototype, emitWarningSync }) {
19+
const { deprecate } = require('internal/util');
20+
21+
const fn = deprecate(
22+
simpleFunction,
23+
'This function is deprecated',
24+
'DEP0000',
25+
emitWarningSync,
26+
!!modifyPrototype,
27+
);
28+
29+
let sum = 0;
30+
bench.start();
31+
for (let i = 0; i < n; ++i) {
32+
sum += fn(i);
33+
}
34+
bench.end(n);
35+
assert.ok(sum);
36+
}
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+17-13Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function pendingDeprecate(fn, msg, code) {
169169
// Mark that a method should not be used.
170170
// Returns a modified function which warns once by default.
171171
// If --no-deprecation is set, then it is a no-op.
172-
function deprecate(fn, msg, code, useEmitSync) {
172+
function deprecate(fn, msg, code, useEmitSync, modifyPrototype = true) {
173173
// Lazy-load to avoid a circular dependency.
174174
if (validateString === undefined)
175175
({ validateString } = require('internal/validators'));
@@ -192,19 +192,23 @@ function deprecate(fn, msg, code, useEmitSync) {
192192
return ReflectApply(fn, this, args);
193193
}
194194

195-
// The wrapper will keep the same prototype as fn to maintain prototype chain
196-
ObjectSetPrototypeOf(deprecated, fn);
197-
if (fn.prototype) {
198-
// Setting this (rather than using Object.setPrototype, as above) ensures
199-
// that calling the unwrapped constructor gives an instanceof the wrapped
200-
// constructor.
201-
deprecated.prototype = fn.prototype;
202-
}
195+
if (modifyPrototype) {
196+
// The wrapper will keep the same prototype as fn to maintain prototype chain
197+
// Modifying the prototype does alter the object chains, and as observed in
198+
// most cases, it slows the code.
199+
ObjectSetPrototypeOf(deprecated, fn);
200+
if (fn.prototype) {
201+
// Setting this (rather than using Object.setPrototype, as above) ensures
202+
// that calling the unwrapped constructor gives an instanceof the wrapped
203+
// constructor.
204+
deprecated.prototype = fn.prototype;
205+
}
203206

204-
ObjectDefineProperty(deprecated, 'length', {
205-
__proto__: null,
206-
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
207-
});
207+
ObjectDefineProperty(deprecated, 'length', {
208+
__proto__: null,
209+
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
210+
});
211+
}
208212

209213
return deprecated;
210214
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.