Skip to content

Instantly share code, notes, and snippets.

@vipinrana
vipinrana / index.js
Created November 28, 2020 15:23
Object.freeze() vs Object.seal() vs Object.preventExtensions()
let obj = {id: 1, name: "John Doe", age: 25};
/**
* Object.freeze()
*
* In freeze object, addition, updation and delete operation cannot be done except read
*
*/
@vipinrana
vipinrana / index.js
Last active November 25, 2020 19:25
Promise.race vs Promise.any
/*
* Promise.race will first settled either fulfilled or rejected from the iterable object
*
* Promise.any find the first fulfilled without concern of reject promise from the iterable object.
* If all are rejected promise then it will result an AggregateError.
*
*/
// On passing first resolve and second reject promise
@vipinrana
vipinrana / sparseIndex.txt
Last active November 21, 2020 13:27
Sparse Index in mongodb
db.collection_name.insert([
{_id: 1, name: "John Doe", age: 25},
{_id: 2, name: "Foo Bar", age: 35},
{_id: 3, name: "Hello World"}
]);
// create sparse index
db.scores.createIndex( { age: 1 } , { sparse: true } );
// count the sparse Indexes Field will output 2
@vipinrana
vipinrana / timing.js
Last active November 27, 2020 06:51
measuring different execution time in node js
const {performance} = require('perf_hooks');
// using hrtime API
const hrBefore = process.hrtime();
setTimeout(function () {
const hrAfter = process.hrtime(hrBefore);
console.log(`Using hrtime ${hrAfter[0] * 1e3 + hrAfter[1] / 1e6}ms`);