Skip to content

Instantly share code, notes, and snippets.

View ishwarrimal's full-sized avatar
💭
Learning

Ish ishwarrimal

💭
Learning
  • intuit
  • Bangalore India
View GitHub Profile
@ishwarrimal
ishwarrimal / LRUCache.js
Created November 12, 2021 14:33
implementation of LRU cache using Map and doubly linked list in JS
//A basic doubly linked list structure
class Node {
constructor(val,prev = null, next = null){
this.val = val;
this.prev = prev;
this.next = next;
}
}
@ishwarrimal
ishwarrimal / lazyLoader.js
Created April 25, 2021 12:56
JS utility to load all the media lazyly
/**
* lazyLoader
* Check for elements in the document to be loaded later when visible to the user.
* @see https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/
* @example
* <element src="" data-src="/url/" data-srcset="..." />
*/
(function (ready) {
if (document.readyState === "complete" || document.readyState === "interactive") {
@ishwarrimal
ishwarrimal / singleton
Created August 25, 2020 17:16
implementing singleton in a simple way in JS
{
let Singleton = (function(){
let instance;
let createInstance = ()=>{
let obj = {name: new Date()}
return obj;
}
return {
getInstance : () => {
@ishwarrimal
ishwarrimal / Debounce
Last active August 24, 2020 10:39
A simple JS implementaiton of debounce
let setTimer = null
let debounce = (cb, delay) => {
clearTimeout(setTimer)
setTimer = setTimeout(cb, delay);
}
@ishwarrimal
ishwarrimal / Throttle
Created August 24, 2020 10:31
Simple implementation of throttle in JS
export function throttle(callback, delay, options = {}) {
let flag = true
return () => {
if (flag) {
flag = false
setTimeout(() => {
callback(options)
flag = true
}, delay)
}
@ishwarrimal
ishwarrimal / queueInJs.js
Created May 21, 2020 10:01
Implementation of Queue in JS
function Queue() {
this.length = 0;
}
Queue.prototype.push = function (item) {
var node = {item: item};
if (this.last) {
this.last = this.last.next = node;
} else {
this.last = this.first = node;
@ishwarrimal
ishwarrimal / confused-this.js
Last active April 28, 2020 09:58
Confusions with this variable's context which arise with constructor invocation pattern for regular and arrow function
function GetValue1(){
this.value = 10;
// using regular funciton to access the varialbe `this.value`
var printValue = function(){
// Since printValue is an independent funciton and not a property of the GetValue1 constructor,
// so when this is called as constructor, printValue points to globalScopr
// where in case of reguar funciton call, this will bound to the execution context of GetValue1
console.log('The value of this.value is, ', this.value) //consoles 20
}
printValue()