JSFLのベンチマークをとってみる
Flash, JSFL ·JSFLのパフォーマンスを向上させる記事があったので、自分でも試してみました。
参考:Salsa de Pixeles: Improving JSFL performance
こういう関数を作って実行してみる。xJSFLにTimer が用意されていて便利。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xjsfl.init(this); // init xJSFL | |
;(function(){ | |
function scriptFolderURI() { | |
return fl.scriptURI.replace(/[¥/][^¥/]*?$/,"/"); | |
} | |
// use Lo-Dash | |
fl.runScript(scriptFolderURI() + "jsload/lib/lodash.compat.js"); | |
})(); | |
function benchmark(name, times, callback){ | |
var timer = new Timer(name); | |
timer.start(); | |
for (var i=0; i < times; i++){ | |
callback(); | |
} | |
timer.stop(true); | |
} | |
clear(); | |
benchmark("benchmark test", 1000,function(){ | |
// task | |
_.reduce(_.range(100), function (sum, num) {return sum + num;}); | |
}); | |
benchmark("benchmark test2", 1000,function(){ | |
// task | |
_.map(_.range(100), function (item) {return item * item * item;}); | |
}); |
結果は以下のようになります。単純な計算なのであまり変わりませんね。
Task "benchmark test" took 126 milliseconds
Task "benchmark test2" took 156 milliseconds
キーフレームの作成方法による違い
空のキーフレームを100個作るときの時間を計測しています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xjsfl.init(this); // init xJSFL | |
;(function(){ | |
function scriptFolderURI() { | |
return fl.scriptURI.replace(/[¥/][^¥/]*?$/,"/"); | |
} | |
// use Lo-Dash | |
fl.runScript(scriptFolderURI() + "jsload/lib/lodash.compat.js"); | |
})(); | |
function benchmark(name, times, callback){ | |
var timer = new Timer(name); | |
timer.start(); | |
for (var i=0; i < times; i++){ | |
callback(); | |
} | |
timer.stop(true); | |
} | |
function deleteAllLayers() { | |
function layerCallback(layer, index) { | |
$timeline.deleteLayer(index) | |
} | |
Iterators.layers(Context.create(), layerCallback); | |
} | |
clear(); | |
benchmark("convert keyframes", 10,function(){ | |
// task | |
$timeline.convertToKeyframes(0,100); | |
deleteAllLayers(); | |
}); | |
benchmark("insert keyframes", 10,function(){ | |
// task | |
_.range(100).map(function(item){$timeline.insertKeyframe(item);}); | |
deleteAllLayers(); | |
}); | |
結果は以下のようになります。 convertToKeyframesで一括で作る場合とinsertKeyframeで1フレームずつ作る場合を比較しましたが、 前者の方が後者より約45倍早くなっています。
Task "convert keyframes" took 565 milliseconds
Task "insert keyframes" took 25.0 seconds
当たり前といえば当たり前のような結果ですが、JSFL書くときは気をつけたいです。