Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active May 29, 2016 04:36
Show Gist options
  • Save nishinoshake/c1d374ed240d100edf70 to your computer and use it in GitHub Desktop.
Save nishinoshake/c1d374ed240d100edf70 to your computer and use it in GitHub Desktop.
javascriptの時間測定
//DOM生成を単純に繰り返すと3000ms
console.time('createNode');
for (var i = 0; i < 100000; i++) {
$('#Main').append('<div>' + i + '</div>');
}
console.timeEnd('createNode');
//キャッシュすると300ms
var dom = '';
for (var i = 0; i < 100000; i++) {
dom += '<div>' + i + '</div>';
}
$('#Main').append(dom);
var $span = $('<span>');
// eachはforの倍以上かかる
$span.each(function() {
$(this).text($(this).text()+1);
});
for ( i = 0, length = $span.length; i < length; i++ ) {
$span.eq(i).text($span.eq(i).text()+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment