Skip to content

Instantly share code, notes, and snippets.

View mraleph's full-sized avatar
🐌

Slava Egorov mraleph

🐌
View GitHub Profile
var https = require('https');
var fs = require('fs');
var util = require('util');
var sys = require('sys');
https.createServer({
key: fs.readFileSync('some.key'), cert: fs.readFileSync('some.crt')
}, function(rq, rs) {
if (rq.url == '/upload' && rq.method.toLowerCase() == 'post') {
rq.on('data', function (buffer) {
v8::Local<v8::Function> MkFunction(v8::Handle<v8::String> body) {
v8::HandleScope scope;
// Get global object
v8::Local<v8::Object> global = v8::Context::GetCurrent()->Global();
// Get built-in Function constructor (see ECMA-262 5th edition 15.3.2)
v8::Local<v8::Function> function_ctor =
v8::Local<v8::Function>::Cast(global->Get(v8::String::New("Function")));
Local<Object> obj = args[1]->ToObject();
Local<Array> names = obj->GetPropertyNames();
uint32_t length = names->Length();
for (uint32_t i = 0; i < length; i++) {
HandleScope loop_scope;
Local<String> name = names->Get(i)->ToString();
Local<Value> value = obj->Get(name);
}
function global_test() {
global.test = function (){
var start = Date.now();
var a = -100000000;
for (var i = 0; i < 1000; i++) {
for (var j = 0; j < 1000; j++) {
a += i + j;
}
}
var end = Date.now();
static coroutine* curr_coroutine; // getspecific, setspecific use this global var to know what is running
void resume (coroutine* target) {
coroutine* self = curr_coroutine;
{ Unlocker unlocker; // archive currently running coroutine
curr_coroutine = target;
switch_context(target);
current_coroutine = self;
} // here ~Unlocker will restore information from curr_coroutine.
}
// This is modified version of benchmark from http://d.hatena.ne.jp/sumim/20110217/p1
//
// It is ~35% faster when I run it on the recent nodejs compiled for ia32 (V8 3.1.6).
//
// mraleph@whitestar|~/src/node/% ./node test.js
// 0
// 3.509
// mraleph@whitestar|~/src/node/% ./node test-original.js
// 0
// 5.344
// NB: this micro-benchmark is stupid in a sense that all loops can be potentially optimized away
// but it's only purpose is to show that variable accesses become slower when eval is in the same scope.
// -- Vyacheslav Egorov
function foo () {
var j = 0;
for (var i = 0; i < 1e7; i++) j = i;
for (var i = 0; i < 1e7; i++) j = i;
for (var i = 0; i < 1e7; i++) j = i;
return j;
String.prototype.a = function () { };
function foo () {
var a = "a";
String.prototype.a = function () { };
for (var i = 0; i < 1e7; i++) {
a.a();
}
}
function baseline() {
var j = 0;
for (var i = 0; i < 1e7; i++) j = i;
for (var i = 0; i < 1e7; i++) j = i;
for (var i = 0; i < 1e7; i++) j = i;
for (var i = 0; i < 1e7; i++) j = i;
}
function with_with() {
with ({}) { }
var s = "qwertyuiop";
for (var i = 0; i < 10; i++) s += s;
s.charCodeAt();
function str2buffer(s) {
var length = s.length;
var b = new Buffer(length);