Javascript 幾種 Array 填充 0 的方式

  1. Array push
    for (var i = 0, a = []; i < 100; i++) a.push(0);
  2. Array assign
    for (var i = 0, a = new Array(100); i < 100;) a[i++] = 0;
  3. Typed array conversion slowest
    [].slice.call(new Uint8Array(100));
  4. Binary concatenation
    for (var i = 100, a = [], add = [0]; i; i >>=1) {
        if (i & 1) a.push.apply(a, add);
        add.push.apply(add, add);
    }
  5. Array Apply
    Array.apply(null, new Array(100)).map(Number.prototype.valueOf,0);
  6. Array.fill() fastest
    new Array(100).fill(0);

 

Responsive iFrames with jQuery

iframe {
  max-width: 100%;
}
function adjustIframes()
{
  $('iframe').each(function(){
    var
    $this       = $(this),
    proportion  = $this.data( 'proportion' ),
    w           = $this.attr('width'),
    actual_w    = $this.width();
    
    if ( ! proportion )
    {
        proportion = $this.attr('height') / w;
        $this.data( 'proportion', proportion );
    }
  
    if ( actual_w != w )
    {
        $this.css( 'height', Math.round( actual_w * proportion ) + 'px' );
    }
  });
}
$(window).on('resize load',adjustIframes);

From: https://gist.github.com/aarongustafson/1313517