Skip to content

Instantly share code, notes, and snippets.

@laverdet
Created February 22, 2011 16:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laverdet/838953 to your computer and use it in GitHub Desktop.
Save laverdet/838953 to your computer and use it in GitHub Desktop.
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);
}
}
}
function copyFileWithoutFiber(from, to, ondone) {
fs.readFile(from, 'utf8', function(err, data) {
if (err) {
ondone(err);
return;
}
fs.writeFile(to, data, 'utf8', function(err, data) {
if (err) {
ondone(err);
} else {
ondone();
}
});
});
}
function copyFileWithFiber(from, to, ondone) {
Fiber(function() {
try {
fs.readFile(from, 'utf8', resumer());
var data = yield();
fs.writeFile(to, data, 'utf8', resumer());
yield();
ondone();
} catch (ex) {
ondone(ex);
}
}).run();
}
copyFileWithoutFiber('srtsratscopy.js', 'honk.js', function(err) {
console.log('done(' + err + ')');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment