JavaScript

Node.js and V8

Getting started guide to Node.js, the server-side JavaScript runtime environment. Node.js is built on top of the Google Chrome V8 JavaScript engine, and it's mainly used to create web servers - but it's not limited to just that.

  • Node.js uses an event driven, non-blocking I/O model
  • MERN(MongoDB, ExpressJS, ReactJS and Node.js) stack

V8 JavaScript Engine

V8 is the name of the JavaScript engine that powers Google Chrome. It's the thing that takes our JavaScript and executes it while browsing with Chrome. V8 provides the runtime environment in which JavaScript executes. The DOM and the other Web Platform APIs are provided by the browser.

  • V8 uses Just-in-time (JIT) compilation, which converts the native JavaScript code to machine code. So, the difference between V8 code and others is that it does not produce any intermediate code.

Runtime

  • the Ignition interpreter compiles the JavaScript code and generates non-optimized machine code.
  • The machine code is analyzed and re-compiled for best performance, by the Turbofan and Crankshaft.
  • Liftoff is responsible for machine code generation in a highly optimized way. It generates code for each opcode and perform way better then Turbofan.
  • Orinoco is responsible for garbage collection. It looks for disconnected memory allocations and perform operations to free up more space. It also update the pointers to new memory locations.

Optional parameters

1
2
3
4
5
6
7
8
9
function buildName(firstName: string, lastName?: string) {
  if (lastName) return firstName + " " + lastName;
  else return firstName;
}

let result1 = buildName("Bob"); // works correctly now
let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters
Expected 1-2 arguments, but got 3.
let result3 = buildName("Bob", "Adams"); // ah, just right

init project

1
npm init

type

typeof

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
console.log(typeof 42);
// Expected output: "number"

console.log(typeof 'blubber');
// Expected output: "string"

console.log(typeof true);
// Expected output: "boolean"

console.log(typeof undeclaredVariable);
// Expected output: "undefined"

console.log(typeof [1, 2, 3]);
// Expected output: "object"

console.log(typeof {"1": 2});
// Expected output: "object"
number
string
boolean
undefined
object
object
undefined

check type

instance

1
2
if( something instanceof xx ){
}

typeof

1
2
if( typeof something === 'string') {
}

array

length

1
2
3
4
const clothing = ['shoes', 'shirts', 'socks', 'sweaters'];

console.log(clothing.length);
// Expected output: 4
4
undefined

delete

remove kth
1
arr.splice(k, 1);

concat

1
2
3
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
undefined

reverse

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// Expected output: "array1:" Array ["one", "two", "three"]

const reversed = array1.reverse();
console.log('reversed:', reversed);
// Expected output: "reversed:" Array ["three", "two", "one"]

// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// Expected output: "array1:" Array ["three", "two", "one"]

last element

1
2
3
arr[arr.length - 1];
arr.slice(-1);
arr.pop();

JSON

from json to object

1
let obj: MyObj = JSON.parse('{ "myString": "string", "myNumber": 4 }');

String

string to number

1
Number("12345.6657");

strip / trim

1
" zzz  zz ".trim()

For

for range

1
2
3
[...Array(5).keys()]

[...Array(5).keys()].map((i) => 2*i)

for in

1
2
3
4
5
6
7
8
for(let key in Collection){
}

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

for of

1
2
for(let value in Collection){
}

for each

1
2
3
const array1 = ['a', 'b', 'c'];

array1.forEach((value, index) => console.log(value, index));
a 0
b 1
c 2
undefined

for stream

1
[...Array(batch_size)].map(func);

regex

construct

  1. /regex/: / is keyword
  2. new RegExp(regex): / is literal

match

1
"GGGG".match(re)

crawler

cheerio

1
2
const response = await axios.get(url);
const $ = cheerio.load(response.data);