Skip to content

Instantly share code, notes, and snippets.

@syaau
syaau / CollectionHolder.java
Created December 16, 2015 05:58
The Java Initializer Pattern for Thread Safe initialization from mutliple sources
/* An example class */
public class CollectionHolder {
private final List collection;
/* A private constructor to create a shadow instance for initization */
private CollectionHolder() {
collection = new ArrayList<>();
}
/* The public constructor */
@syaau
syaau / array_spread_operator_benchmarking.js
Last active January 13, 2017 14:24
nodejs array spread operator benchmarking
/**
* Benchmarking the array modification for redux using different techniques
*/
const ITERATIONS = 500000;
// Create an array with the given length
function range(length) {
const list = [];
for (let i = 0; i < length; i += 1) {
list.push(Math.random());
@syaau
syaau / test-large-promise.js
Created January 15, 2017 12:47
Check if Promise.all with large number of items works or not
function generateDocs(count) {
const docs = [];
for (let i = 0; i < count; i += 1) {
docs.push({
id: i + 1,
name: `Name${i}`,
processed: false,
});
};
@syaau
syaau / ipinfo.sh
Last active February 26, 2017 17:56
Simple script for getting geo information from ip or host name
#!/bin/sh
# Simple script for getting geo information from ip or host name.
# This script uses linux command `host` to get ip address for
# the given domain name. It also depends on `http://ipinfo.io/`
# to get the geo information.
#
# The valid_ip function has been copied directly from
# http://www.linuxjournal.com/content/validating-ip-address-bash-script
#
# INSTALLATION
/**
* A JSON formatter that transforms the incoming
* stream of JSON data (without proper formatting)
* into properly formatted JSON string.
* Usage:
* yourInputStream.pipe(createJSONFormatter()).pipe(yourOutputStream);
*/
const Transform = require('stream').Transform;
@syaau
syaau / vm-ubuntu.sh
Last active July 26, 2017 10:13
Shell script to start and ssh to a linux VBox VM
#!/bin/bash
# Copy this script with run permission inside
# /usr/local/bin/vm-ubuntu
# Script to start and ssh to a VM on Virtual Box
# It starts the VM in headless mode (if not already started)
# This script assumes an Ubuntu Virtual Box named "Ubuntu"
# is already created with NAT network mode with host port
@syaau
syaau / getLocalIP.js
Created August 24, 2017 07:47
Retrieve the first Local IPv4 from the network interfaces
const os = require('os');
export default function getLocalIP() {
const ifaces = os.networkInterfaces();
const ifnames = Object.keys(ifaces);
for (let i = 0; i < ifnames.length; i += 1) {
const aliases = ifaces[ifnames[i]];
for (let j = 0; j < aliases.length; j += 1) {
const iface = aliases[j];
@syaau
syaau / rn-cli.config.js
Last active January 10, 2018 11:38
Painless react-native library development and testing with symlinks
/**
* Keep this file in the project root
*
* Overrides configuration for metro-bundler so that any
* symlinked library (npm link) is automatically added
* to project root (listened for changes). Also all the
* dependencies and peerDependencies are provided through
* `extraNodeModules` options (which is the main problem
* with symlinked libraries in react-native).
*
@syaau
syaau / toJSON.gs
Last active October 16, 2024 00:53
Google App Script to convert sheet data to JSON array
function onOpen() {
var menuEntries = [{ name: "Download JSON", functionName: "convertToJson" }];
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu("Utils", menuEntries);
}
function convertToJson() {
const sheet = SpreadsheetApp.getActiveSheet();
const lastRow = sheet.getLastRow();
@syaau
syaau / javascript-spread-operator-performance.js
Created February 25, 2020 13:47
Check spread operator vs Object.assign performance (with varying key sizes)
// Test object recreate speed on large objects vs small objects
function createObject(propsCount) {
const obj = {};
for (let i = 0; i < propsCount; i += 1) {
obj[`p${i}`] = i;
}
return obj;
}