Lesson 2 of the Node.js Workshop by Rafał Pocztarski at inFullMobile.
- Prev: https://github.com/rsp/nw1
- Next: https://github.com/rsp/nw3
inFullMobile is a digital product design and development studio based in Warsaw, Poland.
Doing mobile, web, IoT and hardware projects from the idea to a final product.
Rafał Pocztarski is a Senior Node.js Developer and Team Leader at inFullMobile.
Programming since 1986, commercially since 1996.
Programming in Node since watching the first Node.js presentation by Ryan Dahl in 2009.
Enjoys writing about Node.js on Stack Overflow, holding a rare Gold Node.js Badge.
The JavaScript global object
(that has all global variables as properties)
is called global
in Node,
window
in the browser
and self
in Web Workers.
During the workshop I forgot about my own module on npm that returns the global object in all of those three environments, to normalize the name:
As I said during the workshop,
the standard Number
type in JavaScript is:
- 64-bit floating point (the IEEE 754 double precision floating-point number - see: ECMA-262 Edition 5.1, Section 8.5 and ECMA-262 Edition 6.0, Section 6.1.6)
More numeric types are defined in WebAssembly, typed arrays and asm.js.
My answer on Stack Overflow with more details:
Template literals (the backtick strings) documentation:
Node compatibility:
Babel for experiments:
Parsing file with a million strings 'x';
using single quotes, double quotes and backticks, using Node 9.5:
$ time node msq.js # parsing million single quotes
real 0m0.305s
user 0m0.252s
sys 0m0.051s
$ time node mdq.js # parsing million double quotes
real 0m0.307s
user 0m0.253s
sys 0m0.051s
$ time node mbt.js # parsing million backticks
real 0m0.686s
user 0m0.526s
sys 0m0.154s
It seems that backtick parsing takes two times longer than single and double quotes but it is still less than a microsecond. This is the speed of compilation that is done once on program startup.
The usage of backticks vs single or double quotes doesn't seem to make any difference during runtime:
$ time node psq.js # processing single quote concats
real 0m1.058s
user 0m0.983s
sys 0m0.042s
$ time node pdq.js # processing double quote concats
real 0m1.054s
user 0m0.984s
sys 0m0.043s
$ time node pbt.js # processing backtick concats
real 0m1.056s
user 0m0.985s
sys 0m0.043s
$ time node pno.js # empty loop for comparison
real 0m0.151s
user 0m0.133s
sys 0m0.016s
The above test was 100 million string concatenations, 15% of the time is the loop overhead so it's about 9 nanoseconds per operation.
The JS files used in the tests are in the repository for reference.
My answers on Stack Overflow on how to deal with rounding errors:
- Avoiding problems with JavaScript's weird decimal calculations
- Node giving strange output on sum of particular float digits
Some libraries for arbitrary precision numbers in JS:
- https://github.com/MikeMcl/big.js/
- https://github.com/MikeMcl/decimal.js/
- https://github.com/charto/bigfloat
- https://github.com/josdejong/mathjs
- https://github.com/vukicevic/crunch
More searches on npm:
- https://www.npmjs.com/browse/keyword/precision
- https://www.npmjs.com/browse/keyword/bignum
- https://www.npmjs.com/browse/keyword/bigint
TC39 BigInt proposal:
TypeScript by Microsoft is JavaScript + static typing.
My module on npm that exposes the AsyncFunction
constructor that is usually unexsposed, as I discussed on the workshop:
Without this module:
> x = function () {};
[Function: x]
> x instanceof Function;
true
> x = async function () {};
[AsyncFunction: x]
> x instanceof AsyncFunction;
ReferenceError: AsyncFunction is not defined
With this module:
> x = function () {};
[Function: x]
> x instanceof Function;
true
> x = async function () {};
[AsyncFunction: x]
> x instanceof AsyncFunction;
true