LoginSignup
0
0

More than 5 years have passed since last update.

データの有効行を2行取得する速度に関して。

Last updated at Posted at 2018-03-03

テストデータ


let data=`






Results update in real-time as you type.
Supports JavaScript & PHP/PCRE RegEx.
Roll over a match or expression for details.
Save & share expressions with others.
Use Tools to explore your results.
Browse the Reference for help & examples.
Undo & Redo with ctrl-Z / Y in editors.
Search for & rate Community patterns.
`;

まず幾つかパターン


let a=data.split(/\r?\n/).filter(d=>d.length!=0).slice(0,2).join('\n')
console.log(a)
let b=data.trim().split(/\r?\n/).slice(0,2).join('\n');
console.log(b)
let c=data.match(/.+\r?\n.*\r?\n/).join('')
console.log(c)
//=>Results update in real-time as you type.
//Supports JavaScript & PHP/PCRE RegEx.

たいてい上述の3つで事足りるが、データ数の見通しがない場合、splitの全分割やregexの*は一度最後までデータを読み取ってしまう。

データの小見出しが欲しい場合、常識の範囲内で先に切ると、一万文字くらいから速度差がでる。

let lim=1000,x=data.slice(0,lim)
let d=x.match(/.+\r?\n.*\r?\n/).join('')||x
console.log(d)

結果

0
0
0

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
0
0