const isReachable = require('is-reachable')
async function isDotReachable() {
const dotReachable = await isReachable('dotjs.io')
console.log(dotReachable)
}
const isReachable = require('is-reachable')
async function isDotReachable() {
const dotReachable = await isReachable('dotjs.io')
console.log(dotReachable)
}
// Don't require the module until you need it
// require() caches results
async function isDotReachable() {
const isReachable = require('is-reachable')
const dotReachable = await isReachable('dotjs.io')
console.log(dotReachable)
}
// Do you even need a module?
function isDotReachable() {
return new Promise((resolve, reject) => {
const http = require('http')
http.get('http://dotjs.io', () => {
resolve()
}).on('error', (e) => {
reject(e)
})
})
}
#!/usr/bin/env node
'use strict';
[...]
try {
require(__dirname + '/../lib/v8-compile-cache.js');
} catch (err) {
}
[...]
require('v8-compile-cache')
async function isDotReachable() {
// The "compiled" blobs are now cached
// Have V8 parse the module only once
const isReachable = require('is-reachable')
const dotReachable = await isReachable('dotjs.io')
console.log(dotReachable)
}
npm install
Turning your code
into pixels
2️⃣
// Option 1
const divs = document.querySelectorAll('.test-class')
// Option 2
const divs = document.getElementsByClassName('test-class')
// 🤔 Which one is faster? And by how much? Let's find out:
// https://jsperf.com/dotjs-perf-example
document.querySelectorAll('.test-class')
±5.27%, 99% slower
document.getElementsByClassName('test-class')
±0.55%
#include <nan.h>
NAN_METHOD(IsPrime) {
if (!info[0]->IsNumber()) {
Nan::ThrowTypeError("Argument must be a number!");
return;
}
int number = (int) info[0]->NumberValue();
if (number < 2) {
info.GetReturnValue().Set(Nan::False());
return;
}
for (int i = 2; i < number; i++) {
if (number % i == 0) {
info.GetReturnValue().Set(Nan::False());
return;
}
}
info.GetReturnValue().Set(Nan::True());
}
NAN_MODULE_INIT(Initialize) {
NAN_EXPORT(target, IsPrime);
}
NODE_MODULE(addon, Initialize);
// Brought to you by Parcel:
// Import a wasm file like it's no big deal
const { add } = await import('./add.wasm')
const result = add(2, 3)
// 🙀 Same trick, but with Rust
const { add } = await import('./add.rs')
const result = add(2, 3)
// Rust, a true teamplayer
#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
return a + b
}
Application
Lifecycle
4️⃣
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
// Suspend all expensive operations
// 🔪 setInterval()
// 🔪 Animations
// 🔪 Not-urgent network requests
} else {
// Continue!
}
})