LoginSignup
5
5

More than 5 years have passed since last update.

JavaScriptコンソールを使うときのバッドノウハウ

Posted at

Firefox と Opera、node のコンソールではオブジェクトの中身をダンプしようとする。
以下、10の要素をもつ Float32Array を変数aに代入した様子。これがもし、100万要素の Float32Array だったら表示に時間がかかる。鬱陶しい。最悪クラッシュする。ちなみに 100万要素は 44.1kHz のモノラルで 23秒 くらいのデータ量。配列でなくても、複雑な木構造のデータとかでも危険なときがある。

chrome-or-safari.js
a = new Float32Array(10)
// > Float32Array
firefox.js
a = new Float32Array(10)
// > ({0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0})
opera.js
a = new Float32Array(10)
// + Float32Array [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
node.js
> a = new Float32Array(10)
{ '0': 0,
  '1': 0,
// ...
  '8': 0,
  '9': 0,
  buffer: 
   { '0': 0,
     '1': 0,
// ...
     '38': 0,
     '39': 0,
     byteLength: 40 },
  BYTES_PER_ELEMENT: 4,
  set: [Function: set],
  length: 10,
  slice: [Function: slice],
  byteOffset: 0,
  byteLength: 40,
  subarray: [Function: subarray] }

回避策

最後に評価されたものをダンプしようとするので、, 0 をつけてあげる。

badknowhow.js
a = new Float32Array(1000000), 0
// > 0
a.length
// > 1000000
5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5