Skip to content

Instantly share code, notes, and snippets.

View samarpanda's full-sized avatar
👋

Samar Panda samarpanda

👋
View GitHub Profile
@samarpanda
samarpanda / gist:4125105
Last active December 1, 2023 08:20
Test your php code execution time and Memory usage
<?php
// from http://php.net/manual/en/function.filesize.php
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
@samarpanda
samarpanda / first-input-delay.md
Last active October 20, 2023 12:24
First input delay (FID)

First input delay(FID)

This is an interactive metric. Collected once user tries to interact with the page.

Using web-vitals npm package

import {getFID} from 'web-vitals';
getFID(console.log);

Static file serving using Nginx + Docker

Quick file serving with lightweight & efficient webserver using nginx:1.25-alpine

Quick project aims to demonstrate the creation, deployment of a lightweight & efficient web server using Nginx running on an Apline Linux based Docker container. The project serve static files from any mounted folder, making an ideal solution for hosting simple website, front-end applications, or any content that needs to be served from a folder / local system.

nginx-serve.sh

  1. Container name CNAME=nginx-8080
  2. Nginx image is used from dockerhub
@samarpanda
samarpanda / generatePassword.js
Last active August 2, 2023 09:35
Random password generator
function getRandomCharacter(charset = "" ){
const randomIndex = Math.floor(Math.random() * charset.length);
return charset.charAt(randomIndex);
}
function generatePassword(length) {
const charsetAlphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$?";
// const charsetNumbers = "0123456789";
// const specialCharacters = "!@#$?"
@samarpanda
samarpanda / Largest-Contentful-Paint.md
Last active June 16, 2023 15:18
Largest contentful Paint

I wonder if i can see all the lcp elements instead of getting the eventual lcp element from the performance panel. Below script higlights all lcp elements available on the page. After running the script in console / source - snippet it highlight by a dotten blue line for all lcp elements. This uses browser's PerformanceObserver API to get all lcp elements.

By @samarpanda

/**
 * PerformanceObserver
 */
@samarpanda
samarpanda / gist:3726988
Created September 15, 2012 08:33
Batch optimise images with a single command
#For jpeg files
#Need to install jpegtran in ubuntu
sudo apt-get install libjpeg-progs
Usage tips:
#To optimise a single jpeg image:
jpegtran -copy none -optimise -outfile image.jpg image.jpg
#To optimise all jpegs in the current directory:
for img in `ls *.jpg`; do jpegtran -copy none -optimise -outfile $img $img; done
@samarpanda
samarpanda / Certificate-creation-using-openssl.md
Last active May 4, 2023 13:37
Self signed certificate creation using openssl

Man in the middle defense: OpenSSL

  • Generate a private key
openssl genrsa -aes128 \
	-out my-private.key 2048
  • Generate a public key from private key
@samarpanda
samarpanda / Sublime Text 2 keyboard shortcut.md
Last active January 26, 2023 16:02
My favorite keyboard shortcuts Sublime Text 2

Keyboard shortcuts for Sublime Text 2

  1. Multi-selection: To enable multi-selection, you have several options
  • Press Alt or Command and then click in each region where you require a cursor.
  • Select a block of line, and then press Shit + Command + L
  • Place the cursor over a particular word, and press Control/Command + D repeatedly to select additional occurences of that word.
  • Alternatively, add an additional cursor at all occurences of a word by typing Alt+F3 on Windows, or Ctrl+Command+G on the Mac.
  1. Find all occurence of a word in a file and edit all of those at a time: > Command + F then Option + Enter. All the occurance of the word should be editable.
@samarpanda
samarpanda / FlushDns.md
Created January 9, 2013 12:42
Flush DNS

Flush DNS OS X 10.8 - 10.7

sudo killall -HUP mDNSResponder

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.