Giter Site home page Giter Site logo

nw2's Introduction

Node Workshop - Lesson 2

=> github.com/rsp/nw2

Lesson 2 of the Node.js Workshop by Rafał Pocztarski at inFullMobile.

About us

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.

https://infullmobile.com/

About me

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.

https://pocztarski.com/

Global object

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:

Number type

As I said during the workshop, the standard Number type in JavaScript is:

More numeric types are defined in WebAssembly, typed arrays and asm.js.

My answer on Stack Overflow with more details:

String literals

Template literals (the backtick strings) documentation:

Node compatibility:

Babel for experiments:

Benchmarks

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.

Number precision

My answers on Stack Overflow on how to deal with rounding errors:

Some libraries for arbitrary precision numbers in JS:

More searches on npm:

TC39 BigInt proposal:

TypeScript

TypeScript by Microsoft is JavaScript + static typing.

Unexposed

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

nw2's People

Contributors

rsp avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.