Skip to content

Instantly share code, notes, and snippets.

@srikumarks
srikumarks / cspdemo2-pike.html
Last active December 19, 2015 17:39
CSP demo 2 - Rob Pike's example
<button id="search">Search</button>
<div id="results"></div>
<script src="IO.js"></script>
<script src="IO.Browser.js"></script>
<script>
var fake_search = function (kind) {
return function (query) {
return IO.do([
function (_) {
return IO.delay(Math.floor(Math.random() * 100));
@srikumarks
srikumarks / peak-callback-rate.html
Created July 20, 2013 03:11
Quick-n-dirty measurement of the peak callback rate possible in JS.
<script>
// Measuring the rate at which we can get callbacks.
// This technique is known, I believe, to yield the
// fastest call-me-as-soon-as-you-can time.
var start_ms = performance.now(), end_ms;
var img = document.createElement('img');
var count = 0, N = 10000;
function callback() {
count++;
if (count === N) {
@srikumarks
srikumarks / audio_data_duplex_throughput.c
Last active January 27, 2024 05:41
A simple C program for testing the data throughput between a parent and a child process. The purpose is to estimate how many typical audio buffers of length 4096 float32 samples can be sent between two processes in duplex mode. Note: Code will need to be adapted for other OSes.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Code from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
#include <mach/mach_time.h>
#define ORWL_NANO (+1.0E-9)
#define ORWL_GIGA UINT64_C(1000000000)
static double orwl_timebase = 0.0;
@srikumarks
srikumarks / blas_randmatmul.js
Last active December 21, 2015 03:09
BLAS-based matrix multiplication in node.js (for Julia micro benchmark)
// Requirements:
// 1) npm install ffi
// 2) npm install ref
// 3) dynamically loadable libblas must be available aleady.
var ffi = require('ffi');
var ref = require('ref');
var doublePtr = ref.refType(ref.types.double);
var blas = new ffi.Library('libblas', {
'cblas_dgemm': ['void', [
'int', 'int', 'int', 'int', 'int', 'int',
@srikumarks
srikumarks / channel.js
Last active March 5, 2018 23:06
CSP-style channel implementation for Javascript on top of ES6-style Promises.
"use strict";
// Channel is a simple object that can be used for
// CSP-style concurrency in Javascript. In JS code,
// the act of taking a value from a channel looks
// like a blocking call and therefore is not appropriate
// for single-process/single-thread environments like
// NodeJS. To address that, Channel produces promises
// for values.
//
@srikumarks
srikumarks / thread.js
Last active January 4, 2016 18:39
Async helper library for ES6 Javascript with generator support
// A thread module for working with ES6 generator functions
// that model threads of execution. This module hopes for
// composeable behaviour.
// fork(g, resume, [context])
//
// Will start the given generator function to run asynchronously.
// When the thread completes, either by an error or by returning
// a value, the given (optional) resume callback is called.
//
@srikumarks
srikumarks / doxbee_thread.js
Created January 28, 2014 07:36
Example of doxbee code written using thread.js
var thread = require('./thread'); // Refers to https://gist.github.com/srikumarks/8662298
// call upload like this -
// thread.fork(upload(stream, idOrPath, tag), optionalResume)
function upload(stream, idOrPath, tag) {
return function* (resume) {
var blob = blobManager.create(account);
var tx = db.begin();
try {
@srikumarks
srikumarks / GraphNodeSet_example.js
Last active August 29, 2015 13:56
Example of using Steller's GraphNodeSet's graph serialization/deserialization.
define('node_types', ['steller'], function (S) {
return {
sine: function (freq) {
var osc = this.audioContext.createOscillator();
osc.frequency.value = freq;
osc.start(0);
return S.SoundModel(this, [], [osc]);
},
gain: function (multiplier) {
var gain = this.audioContext.createGain();
// For completeness, I include the steller API object and the current audio context as arguments.
function theremin(steller, sh) {
var osc = null;
var gain = sh.audioContext.createGain();
var sm = steller.SoundModel({}, [], [gain]);
sm.start = sh.fire(function (clock) {
if (osc) { osc.stop(clock.t1); }
osc = AC.createOscillator();
osc.frequency.value = sm.frequency.value;
@srikumarks
srikumarks / resolveCtx.js
Created March 29, 2014 02:28
sweetjs's resolveCtx rewritten recursively
// (Syntax) -> String
function resolveCtx(originalName, ctx, stop_spine, stop_branch) {
if (ctx instanceof Mark) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
if (ctx instanceof Def) {
if (stop_spine.indexOf(ctx.defctx) !== -1) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
return resolveCtx(originalName, renames(ctx.defctx, ctx.context, originalName), stop_spine, unionEl(stop_branch, ctx.defctx));