codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Question regarding to production mode optimisation. #14

Closed hung-phan closed 9 years ago

hung-phan commented 9 years ago

I have some questions regarding to have babel-plugin-typecheck running in production mode. Is there any way that I can cut off the overheads of type checking in production mode, which means the type check will be enable in development only. My current config for .babelrc is:

{
  "stage": 0,
  "plugins": ["typecheck"],
  "optional": ["runtime"]
}

And my production config I include new webpack.optimize.UglifyJsPlugin(). However, I got the production code compiled to the following:

      if ("number" != typeof t) throw new TypeError("Value of argument 'a' violates contract, expected number got " + (null === t ? "null" : t instanceof Object && t.constructor ? t.constructor.name : typeof t));
      if ("number" != typeof e) throw new TypeError("Value of argument 'b' violates contract, expected number got " + (null === e ? "null" : e instanceof Object && e.constructor ? e.constructor.name : typeof e));

Any idea on this? Also thanks for this wonderful plugin.

phpnode commented 9 years ago

If you remove typecheck from your .babelrc and compile your code, everything will work as normal without the typechecks, but I agree this isn't very convenient. It comes down to the inability to pass options to babel plugins which should be changing soon

phpnode commented 9 years ago

Another point though is that is the overhead real or imagined? The vast, overwhelming majority or apps can get away with leaving this on and it will make no discernible difference to performance at all.

hung-phan commented 9 years ago

Thanks @phpnode, maybe I overthink about it.

necolas commented 9 years ago

If you remove typecheck from your .babelrc and compile your code, everything will work as normal without the typechecks

What is removing the type annotations in this case?

Another point though is that is the overhead real or imagined? The vast, overwhelming majority or apps can get away with leaving this on and it will make no discernible difference to performance at all.

When you have several 1000 modules with type annotations, it will make a difference to bundle size. And it's questionable to be throwing those errors in production.

necolas commented 9 years ago

Oh, TIL Babel strips Flow types by default :)

phpnode commented 9 years ago

it's questionable to be throwing those errors in production.

Well, at least with this enabled then you'll get an early error, so typically you can find the source of the bug faster. With typechecks disabled you'll either get an error further down the stack or unwanted behaviour and it'll be much harder to track down.