Skip to content

Instantly share code, notes, and snippets.

View alemures's full-sized avatar

Alejandro Santiago alemures

View GitHub Profile
@alemures
alemures / async-comparison.js
Last active April 20, 2016 15:35
Comparison between different ways to implement async methods in Node.js
'use strict';
const async = require('async');
const Promise = require('bluebird');
const db = {
app: [
{ id: 10, name: 'twitter' },
{ id: 11, name: 'facebook' },
{ id: 12, name: 'google' }
@alemures
alemures / AsyncFuture.java
Last active May 21, 2023 09:22
Async Future with callback in Java
package com.test;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AsyncFuture {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
@alemures
alemures / yield-promise.js
Created June 23, 2016 14:02
Yield Promises
function run(generator) {
var it = generator();
var obj = it.next();
(function _run() {
if (!obj.done) {
obj.value.then(result => {
obj = it.next(result);
_run();
}).catch(err => it.throw(err));
@alemures
alemures / services.md
Last active January 19, 2017 12:54
High performance Web Services
@alemures
alemures / doubleToRawLongBits.c
Last active September 15, 2016 10:58
Convert a double to long in ANSI C (Java's doubleToRawLongBits)
// Make sure that unsigned long is 8 bytes of length
unsigned long utilDoubleToLong(double value) {
unsigned long longValue;
memcpy(&longValue, &value, sizeof(unsigned long));
return longValue;
}
double utilLongToDouble(unsigned long value) {
double doubleValue;
@alemures
alemures / data-serialization-formats.md
Last active November 29, 2016 11:35
High performance data serialization formats
  • FlatBuffers (Binary)
  • Protocol Buffers (Binary)
  • MessagePack (Binary)
  • Thrift (Binary)
  • Avro (Binary)
  • JSON (human-readable)
@alemures
alemures / elasticsearch-response-string.js
Created November 30, 2016 17:13
Elasticsearch response as string with Node.js client library
// Working with elasticsearch client 12.1.0
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: '127.0.0.1:9200',
log: 'warning'
});
// Override per default deserialize method without JSON.parse()
client.transport.serializer.deserialize = function (str) {
@alemures
alemures / makefile
Last active January 25, 2017 18:04
Support multiple build types with GNU make
#
# Compiler flags
#
CC = gcc
CFLAGS = -Wall -Werror -Wextra
#
# Project files
#
SOURCES = file1.c file2.c file3.c file4.c
@alemures
alemures / ArrayDeque.js
Last active June 22, 2017 18:37
High performance double-ended queue in Javascript
'use strict';
var INITIAL_CAPACITY = 16;
/**
* @param {Array|Number} arg Content or initial capacity.
*/
function ArrayDeque(arg) {
this._elements = new Array(typeof arg === 'number' ?
this._nextHighestPowerOfTwo(arg) : Array.isArray(arg) ?
@alemures
alemures / uuid.js
Created March 24, 2017 16:02
Fast UUID v4 Generator in Javascript
/**
* Fast UUID generator, RFC4122 version 4 compliant.
* @author Jeff Ward (jcward.com).
* @license MIT license
* @link http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
**/
var UUID = (function() {
var self = {};
var lut = []; for (var i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); }
self.generate = function() {