use strict
使基准测试更?
//
// RUN WITH AND WITHOUT THIS
//
"use strict";
var assert = require('assert');
var slice = [].slice;
function thunkify_fast(fn){
assert('function' == typeof fn, 'function required');
return function(){
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
var ctx = this;
return function(done){
var called;
args.push(function(){
if (called) return;
called = true;
done.apply(null, arguments);
});
try {
fn.apply(ctx, args);
} catch (err) {
done(err);
}
}
}
};
function thunkify_slow(fn){
assert('function' == typeof fn, 'function required');
return function(){
var args = slice.call(arguments);
var ctx = this;
return function(done){
var called;
args.push(function(){
if (called) return;
called = true;
done.apply(null, arguments);
});
try {
fn.apply(ctx, args);
} catch (err) {
done(err);
}
}
}
};
var fn = function () { };
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
//
// Only one wrapper can be sent through the optimized compiler
//
suite.add( 'thunkify#fast', function () { thunkify_fast(fn)(function(){}) } )
.add( 'thunkify#slow', function () { thunkify_slow(fn)(function(){}) } )
.on('cycle', function(event) { console.log(String(event.target)); })
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run();
"use strict"
$ node --allow-natives-syntax test.js
thunkify#fast x 8,511,605 ops/sec ±1.22% (95 runs sampled)
thunkify#slow x 4,579,633 ops/sec ±0.68% (96 runs sampled)
Fastest is thunkify#fast
然而,有了这个"use strict;"
$ node --allow-natives-syntax test.js
thunkify#fast x 9,372,375 ops/sec ±0.45% (100 runs sampled)
thunkify#slow x 1,483,664 ops/sec ±0.93% (96 runs sampled)
Fastest is thunkify#fast
如果改变测试用例,
var f_fast = thunkify_fast(fn);
var f_slow = thunkify_slow(fn);
suite.add( 'thunkify#fast', function () { f_fast(function(){}) } )
.add( 'thunkify#slow', function () { f_slow(function(){}) } )
.on('cycle', function(event) { console.log(String(event.target)); })
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run();
从而删除调用thunkify
thunkify#fast x 18,910,556 ops/sec ±0.61% (100 runs sampled)
thunkify#slow x 5,148,036 ops/sec ±0.40% (100 runs sampled)
thunkify#fast x 19,485,652 ops/sec ±1.27% (99 runs sampled)
thunkify#slow x 1,608,235 ops/sec ±3.37% (93 runs sampled)
可以尝试下:
<script type="text/javascript">
window.sitescriptdata = {};
window.sitescriptdata.foo = ( <?php echo json_encode( $structure ); ?> );
</script>
对于基本内容,可以节省执行Ajax回调的操作:
<div id="foobar">
<div><object class="metaobject">
<param name="data" value="<?php echo htmlentities(json_encode($data), ENT_QUOTES );?>" />
</object></div>
</div>
倾向于使用这个JSON对象:
json_encode($data);