Skip to content

Instantly share code, notes, and snippets.

@laverdet
laverdet / nextTick.js
Created March 29, 2011 23:49
nextTick will ALWAYS fire before a setTimeout from the same tick
setTimeout(function() {
console.log('timeout');
}, 0);
// simulate extreme cpu load
var d = +new Date;
console.log(new Date);
while (d + 2000 > new Date);
console.log(new Date);
// Bad third-party code
var fs = require('fs');
require('fibers');
function fetchSomething() { // don't do this!
var fiber = Fiber.current;
fs.readFile(__filename, 'utf8', function(err, val) {
fiber.run(val);
});
return Fiber.yield();
@laverdet
laverdet / fibonacci.js
Created August 4, 2011 05:47
Fibonacci Generator
require('fibers');
// Generator function. Returns a function which returns incrementing
// Fibonacci numbers with each call.
function Fibonacci() {
// Create a new fiber which yields sequential Fibonacci numbers
var fiber = Fiber(function() {
Fiber.yield(0); // F(0) -> 0
var prev = 0, curr = 1;
while (true) {
@laverdet
laverdet / rimraf.js
Created August 8, 2011 02:12
rimraf with futures
var path = require('path'),
fs = require('fs'),
Future = require('fibers/future');
// Create future-returning fs functions
var fs2 = {};
for (var ii in fs) {
fs2[ii] = Future.wrap(fs[ii]);
}
@laverdet
laverdet / newAndRun.diff
Created March 31, 2012 02:42
new Fiber + run() in same C++ function
diff --git a/src/fibers.cc b/src/fibers.cc
index 49a20f7..c446f9d 100644
--- a/src/fibers.cc
+++ b/src/fibers.cc
@@ -143,6 +143,42 @@ class Fiber {
}
/**
+ * Build a new fiber and immediately run it.
+ */
diff --git a/binding.gyp b/binding.gyp
index 379026f..0413e95 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -6,6 +6,7 @@
# Replace gyp platform with node platform, blech
['platform == "mac"', {'variables': {'platform': 'darwin'}}],
['platform == "win"', {'variables': {'platform': 'win32'}}],
+ ['platform == "solaris"', {'variables': {'platform': 'sunos'}}],
],
@laverdet
laverdet / README
Created October 9, 2015 20:38
screeps cpu migration
Put this at the top of your main.js file. Log `extraCPU` at the end to see how much extra CPU will be spent.
If you're using the "loop" architecture you have to move some stuff around. Just reset `extraCPU` and `called` every tick.
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
// simple print utility; mostly lifted from shell.cc
Handle<Value> Print(const Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
@laverdet
laverdet / copy.js
Created February 22, 2011 16:45
compare copying a file natively, and with fibers
require('fibers');
var fs = require('fs');
function resumer() {
var fiber = Fiber.current;
return function(err, val) {
if (err) {
fiber.throwInto(err);
} else {
fiber.run(val);
@laverdet
laverdet / index.js
Created March 10, 2016 22:39
Find a very long path in Screeps
function longPath(from, to) {
let rooms = {};
Game.map.findRoute(from.roomName, to.roomName).forEach(function(step, ii, route) {
function generateCostMatrix(dir1, dir2) {
let cm = new PathFinder.CostMatrix;
let dir = 1 << dir1 | 1 << dir2;
for (let ii = 0; ii < 49; ++ii) {
if (!(dir & 1 << TOP)) {
cm.set(ii, 0, 0xff);
}