1

Is there any perf difference between:

const myVar = myFunc()
export default myVar

and:

export default myFunc()

I prefer the 2nd notation (more concise, less code), but I don't know if it has an impact on performance. Each code loading this module will point to the same reference, or to a new one ?

soywod
  • 4,377
  • 3
  • 26
  • 47
  • It's the same `myFunc()` will only be called once. – Keith Nov 13 '18 at 12:15
  • 1
    *"Each code loading this module will point to the same reference, or to a new one ?"* Modules are always only evaluated once (the first time they are loaded). – Felix Kling Nov 13 '18 at 17:23

2 Answers2

1

There is no difference in this case.

You simply use an alias.

So the reference will be the same each time.

Eugene Mihaylin
  • 1,736
  • 3
  • 16
  • 31
  • Okey, and is the 2nd considered as a bad practice ? I don't see this often, but I like the idea. Even with `export default function() { ... }`, but seems not so common. – soywod Nov 14 '18 at 08:28
  • @soywod The most common (and good, my opinion) practice is to export 1 object from a file, which contents a list of props. So you will have a singleton interface for a file. And all your imports will have the same structure. And they are expandable better. As for exporting only 1 thing for a file it's no a big sence if you use 1st or 2nd, but the better option will be an object with 1 field (for same import/export structure) – Eugene Mihaylin Nov 14 '18 at 09:07
  • I see it a bit differently. The fact to use default export force yourself to apply the Single Responsibility pattern. I have never more than one export to do by file (except for types in fact, so I use `import myFunc, {MyType} from ...`). That why I often use the default, and that's why I came to this `export default function() { ... }`. – soywod Nov 14 '18 at 10:26
1

You can find really good explanations here: https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export#Using_the_default_export and What is "export default" in javascript?

squeekyDave
  • 918
  • 2
  • 16
  • 35