question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Excessing object wrapping.

See original GitHub issue

I’m noticing with webpack v3 there’s a lot of this kind of generated code:

Object(__WEBPACK_IMPORTED_MODULE_3__module_resolve_filename_js__["a" /* default */])(foo)

What’s the reason for wrapping all references with Object() and can this be disabled or optimized away to a single wrap?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

32reactions
sokracommented, Aug 30, 2017

The reason is to call the function with the correct this context:

import x from "module";

x(foo); // <- called without this (undefined/root)
var __WEBPACK_IMPORTED_MODULE_1__module__ = __webpack_require__(1);

__WEBPACK_IMPORTED_MODULE_1__module__.default(foo);
// ^ called with this = __WEBPACK_IMPORTED_MODULE_1__module__

Object(__WEBPACK_IMPORTED_MODULE_1__module__.default)(foo);
// ^ called without this (undefined/root)

The Object() is generated when calling a imported symbol directly. I omitted this in an older version, assuming nobody care about this in an exported function, but this breaks when exporting native functions like setTimeout with expected no this.

Most of these are optimized away with the ModuleConcatenationPlugin.


A bit of history of this: (This started 1.5 years ago)

  • I started to use (0, a.b)() for this, but this broken because of ASI when the previous line has no semicolon…
  • I tried a.b.bind(undefined)() and also a.b.bind()() but bind is pretty slow.
  • I switched to a.b.call(undefined) as it’s a bit faster.
  • I switched to __webpack_require__.i(a.b)() with a identity function
  • I switched to Object(a.b)() because Object behaves like identity for functions and doesn’t require a custom function in the runtime.
  • I did some performance measurements:

Here are up-to-date results:

// chrome  60.0.3112.113
call x 1000000: 9ms
call with Object x 1000000: 24ms
call with comma operator x 1000000: 10ms
call with identity function x 1000000: 9ms
call with valueOf x 1000000: 15ms
call with .bind x 1000000: 45ms
call with own getter x 1000000: 12ms
call with .call x 1000000: 10ms
call cached version x 1000000: 8ms

// firefox 55.0.3 (32-Bit)
call x 1000000: 4.65ms
call with Object x 1000000: 33.15ms
call with comma operator x 1000000: 4.26ms
call with identity function x 1000000: 5.67ms
call with valueOf x 1000000: 5.26ms
call with .bind x 1000000: 183.52ms
call with own getter x 1000000: 5.49ms
call with .call x 1000000: 5.62ms
call cached version x 1000000: 4.57ms

// node 8.4.0
call x 1000000: 10.145ms
call with Object x 1000000: 22.028ms
call with comma operator x 1000000: 9.233ms
call with identity function x 1000000: 10.348ms
call with valueOf x 1000000: 16.490ms
call with .bind x 1000000: 43.926ms
call with own getter x 1000000: 10.633ms
call with .call x 1000000: 10.075ms
call cached version x 1000000: 8.391ms

// node 6.11.2
call x 1000000: 17.505ms
call with Object x 1000000: 23.144ms
call with comma operator x 1000000: 9.383ms
call with identity function x 1000000: 9.346ms
call with valueOf x 1000000: 18.307ms
call with .bind x 1000000: 823.117ms
call with own getter x 1000000: 9.449ms
call with .call x 1000000: 17.538ms
call cached version x 1000000: 7.075ms

Note: bind got faster in node 8, bind is slow in firefox, most calls are faster in FF than in Chrome, Object() is also a bit slow.

  • I noticed that Object() is slower than a identity function, but 30ms for 1 million calls… who cares?
  • I maybe switch back to an identity function
  • I could optimize cases where this is not used, or an arrow function is used.
16reactions
bmeurercommented, Aug 31, 2017

@sokra I see, thanks for the clarification. Will land the Object call inlining today, so Chrome M63 and some future version of Node should have Object(o.f)() as fast as const f = o.f; f().

Wrote down some details here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrapping bumper with several raised objects - Teckwrap
As soon as it is done, remove the excess film and the masking tape, then seal the edge with the hard part of...
Read more >
How to Shrink Wrap Large Objects - YouTube
How to Shrink wrap large objects is a step by step how to video for educational purposes. The animated short video is fun...
Read more >
Let's Talk Shop: Wrapping Unique Objects - GRAPHICS PRO
Over the years we've had some interesting objects come in to be wrapped. ... vinyl to the object and cut away any excess...
Read more >
5 Ways to Wrap Cylindrical Gifts - wikiHow
1. Measure and cut the paper. When you are wrapping a cylinder-shaped gift, you can easily measure out the amount of paper using...
Read more >
DIY: How to Wrap Odd-Shaped Gifts (UPDATED) | Zazzle Ideas
First, measure the width of the object you wish to wrap, add an inch to that measurement, and cut. Overlap the wrapping paper...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found