Skip to content

Instantly share code, notes, and snippets.

@danielsan
danielsan / bad-example.mjs
Created October 17, 2023 14:21
Someone said PHP is faster than Javascript
console.time('script');
const numbers = Array.from({ length: 10_000_000 }, (_, i) => i);
let sum = 0;
numbers.forEach((number) => {
sum += number;
});
console.timeEnd('script');
@danielsan
danielsan / sequential-vs-concurrent.go
Last active October 7, 2023 22:36
Example of sequential calls vs concurrent calls in Javascript. It will work in nodejs18.x or greater or latest versions of chrome/chromium browsers
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
)
@danielsan
danielsan / aws-sdkv2-s3-list-all-objects-with-suffix.mjs
Last active June 1, 2023 22:47
Lists all the content of a given Bucket and Prefix and matches with a RegExp to filter the desired results
import S3 from 'aws-sdk/clients/s3.js'
export const s3 = new S3()
const myFilterContents = (s3Response, pattern) => s3Response.Contents.filter(s3Object => pattern.test(s3Object.Key))
export async function listObjectsWithSuffix (Bucket, Prefix, pattern) {
let myResults = []
let myS3Response
@danielsan
danielsan / promise-magic.js
Last active May 21, 2020 19:18
promise-magic
const sleep = (n, ...args) => new Promise((resolve) => { setTimeout(resolve, n, ...args) })
const sleepError = (n, error) => ({
timeout: null,
prom: null,
clearMyTimeout () {
clearTimeout(this.timeout)
this.prom && this.prom.resolve()
},
@danielsan
danielsan / README.md
Created May 17, 2020 18:31
Answer to stackoverflow 61825263
@danielsan
danielsan / short-json.js
Created April 15, 2020 19:27
short-json has the goal of reducing the size in bytes of a JSON string for network transportation
var o = {"count":-1,"countTime":16274,"queryTime":23158,"data":[{"_id":"15869731151124671.9575aahr","journey":{"fullRoute":"RSW>YHZ","priceSpecification":{"priceBeforeTaxesAndFees":null,"totalPrice":343,"taxAmount":null,"isSoldOut":false,"currencyCode":"CAD","currencySymbol":"CAD","priceOffers":["JOURNEY"]},"passengerDetails":[{"category":"adult","mappedTo":"adultCount","count":1}],"passenger":{"count":1,"seniorCount":0,"adultCount":1,"youngAdultCount":0,"childCount":0,"infantInLapCount":0,"infantInSeatCount":0},"outboundFlight":{"boundType":"OUTBOUND","route":"RSW>YHZ","priceSpecification":{"totalPrice":343,"taxAmount":null},"fareClassInput":"ECONOMY","fareClass":"ECONOMY","departureAirportIataCode":"RSW","arrivalAirportIataCode":"YHZ","departureDate":"2020-06-14","departureCity":{"name":"FMY","cityCode":null},"departureCountry":{"name":"United States","isoCode":null},"arrivalCity":{"name":"YHZ","cityCode":null},"arrivalCountry":{"name":"Canada","isoCode":null},"flightType":"INTERNATIONAL"},"departureDate":"
@danielsan
danielsan / doIt.js
Last active April 4, 2020 14:26
map athena partition for redshift backups
var s3 = new (require('aws-sdk/clients/s3'))()
var doIt = async (m, days, y = new Date().getFullYear()) => {
for (const d of days) {
await s3.listObjectsV2({ MaxKeys: 1000, Bucket: 'em-data-analytics-bucket', Prefix:`redshift-backups/public/em_farenet2_001/year=${y}/month=${m}/day=${d}/airlineiatacode=` }).promise()
.then((res) => {
const m = res.Contents.reduce((acc, i) => acc.add(path.dirname(i.Key)), new Set()); console.log(Array.from(m.values()).map((s, i) => {
const { year, month, day, airl, jt } = s.substr(24).match(/(?<tb>\w+)\/year=(?<year>\w+)\/month=(?<month>\w+)\/day=(?<day>\w+)\/\w+=(?<airl>\w+)\/\w+=(?<jt>\w+)/).groups;
return `PARTITION (year = '${year}', month = '${month}', day='${day}', airlineiatacode='${airl}', journeytype='${jt}') ${i % 4 === 0 ? '\n' : ''}`
}).join(' '))
})
@danielsan
danielsan / promise-all.js
Created May 25, 2018 16:07
Promise.all example
const createAPromise = s => new Promise((resolve) => {
console.log(`this promise will resolve in ${s} seconds`);
setTimeout(() => {
resolve(s);
console.log(`promise solved after ${s} seconds`);
}, s * 1000);
});
const promises = [];
# creating a fake list
cities = ['Miami', 'Diadema', 'NYC', 'Sao Paulo']
fruits = ['Manana', 'Molango', 'Bacate', 'Acabaxi']
list = []
cities.each{|city| fruits.each{|fruit| list.push({city: city, product: fruit, count: rand(10..100)}) }}
puts list
@danielsan
danielsan / gist:3925c9829783a91de7885022b55f432c
Last active July 23, 2016 19:30
sorted by directory name and re-formatted du -hd 1 (disk user human formatted --max-depth=1)
sudo du -hd 1|perl -e 'use Data::Dumper;my %h;while(<>){if(/([\d\.]+\w)\s*(\.\/.+)$/){$h{$2}=$1;}}; for my $k(sort(keys(%h))){printf("%4s | %s\n", $h{$k}, $k)};'