Skip to content

Instantly share code, notes, and snippets.

View DronRathore's full-sized avatar

Dron Rathore DronRathore

View GitHub Profile
@DronRathore
DronRathore / anonymous.js
Created September 18, 2014 14:31
Node Anonymous Function Vs Named Functions
var globalTime = new Date().getTime();
var b = function(callback){
var date = new Date().getTime();
for (var i=0;i<10000;i++)
callback();
console.log("Completed a function test in", new Date().getTime()-date);
}
b(function(){
var date = new Date().getTime();
//console.log("Started A at:", date-globalTime);
@DronRathore
DronRathore / call_apply.js
Last active August 29, 2015 14:14
Call vs Apply
var classA = function(options){
if (this instanceof classA){
this.options = options;
return this
} else {
return new classA(options)
}
}
classA.prototype.someAsnyc = function(callback, flag){
@DronRathore
DronRathore / simple_timeout.js
Created February 8, 2015 16:47
Simple Timeout
var time = Date.now();
setTimeout(function(){console.log("I was fired after: ", Date.now()-time, "ms")}, 1);
/*
Output = I was fired after: 1 ms
*/
@DronRathore
DronRathore / law_proof.js
Last active August 29, 2015 14:15
setTimeout = setInterval
/*
Law: clearInterval = clearTimeout
*/
var a = setTimeout(function(){console.log(1)}, 100);
clearInterval(a);
var b = setInterval(function(){console.log(1)}, 100);
clearTimeout(b);
/*
Nothing will be logged on console!
We cleared the timers!
@DronRathore
DronRathore / animation_frame_sample.js
Created February 8, 2015 18:18
requestAnimationFrame
/*
Sample from MDN
*/
var start = null;
var element = document.getElementById("SomeElementYouWantToAnimate");
function step(timestamp) {
if (!start) start = timestamp;
var progress = timestamp - start;
element.style.left = Math.min(progress/10, 200) + "px";
@DronRathore
DronRathore / array.js
Created August 20, 2015 13:01
Array Init vs Array.push
/*
Adding values in array at init using variables is faster
then processing individual entity
*/
function A(options){
var a = [ “something” + options.A + “more”, “second elem” + options.B + “Yo!”]
return A;
}
function B(options){
var a = [];
@DronRathore
DronRathore / object_id.js
Created August 21, 2015 14:52
Generate Unique Object IDs
/*
Generate unique IDs for Objects
*/
(
function(ref){
ref.__counting_tick_var = 0;
Object.prototype.id = function(){
return (
this.__counting_tick_var ?
this.__counting_tick_var :
@DronRathore
DronRathore / clean.sh
Last active November 3, 2015 17:20
Removes .DS_STORE and underscore files from any folder
#!/bin/bash
# Browsing directories on mac creates .DS_STORE and further
# it create similar files for any file you open
# it is annoying to see such files in a github repo folder and often
# we end up pushing them in our codebase
# so here is a one liner solution to remove .DS_STORE and other ._files from git
# run junk for non git repo, or cg for git repo, you can add this in your .bash_profile too.
# Add the below lines in your .bash_profile, run source ~/.bash_profile and done
@DronRathore
DronRathore / express_router_regex.go
Last active September 17, 2016 19:56
Convert Go http routes to express-js fashion
package main
import "os"
import "text/tabwriter"
import "regexp"
import "fmt"
func main(){
// silent the groups to eliminate redundant data in match
var r = compileRegex("/:name/:service/:id(?:[0-9]{0,3})")
var regex = r.FindStringSubmatch("/api/buy/123")
@DronRathore
DronRathore / binary_tree_mod.cc
Created May 29, 2017 09:09
Binary Search Tree
// Author: Dron Rathore <dron(.)rathore[(@)]G-Mail>
// A modified C++ implementation for Binary Search Tree
// Every node keeps a parent node ref to help the deletion
// in Θ(log(n)) complexity.
// This is no rocket science, same can be acheived with
// double pointers where you perform a look ahead search in
// the tree in case of deletion so you have parent of the
// Right Subtree's largest node which comes handy.