Non-blocking JavaScript - 1

2021. 6. 17. 16:32·과거
반응형

병렬성


동시성
동시성
동시성


JavaScript Concurreny


setTimer

const Item = class {
    time;
    block;
    constructor(block, time) {
    	this.block = block;
        this.time = performance.now();
    }
}

const queue = new Set;

const f = time => {
	queue.forEach(item => {
    	if (item.time > time) return;
        queue.delete(item);
        item.block();
    });
    requestAnimationFrame(f);
};
requestAnimationFrame(f);

const timeout = (block, time) => queue.add(new Item(block, time));
timeout(_ => console.log("hello"), 1000);

위 코드의 도식화
JavaScript Concurrency의 일부에서 동작


Non Blocking For


// blocking for
const working = _ =>{};
for (let i = 0; i < 100000; i++) working();

// non-blocking for
const nbFor = (max, load, block) => {
    let i = 0;
    const f = time => {
        let curr = load;
        while(curr-- && i < max) {
            block();
            i++;
        }
        console.log(i);
        if (i < max - 1) timeout(f, 0);
    }
    timeout(f, 0);
};

nbFor(100, 10, working);

Generator

const gene = function* (max, load, block) => {
	let i = 0, curr = load;
    while (i < max) {
        if (curr--) {
            block();
            i++;
        } else {
            curr = load;
            console.log(i);
            yield;
        }
    }
};

const nbFor = (max, load, block) => {
    const iterator = gene(max, load, block);
    const f = _ => iterator.next().done || timeout(f, 0);
    timeout(f, 0);
};

nbFor(100, 10, working);

Promise

const gene2 = function* (max, load, block) {
  let i = 0;
  while (i < max) {
    yield new Promise(res => {
      let curr = load;
      while (curr-- && i < max) {
        block();
        i++;
      }
      console.log(i);
      timeout(res, 0);
    });
  }
};

const nbFor = (max, load, block) => {
  const iterator = gene2(max, load, block);
  const next = ({value, done}) => done || value.then(v => next(iterator.next()));
  const next(iterator.next());
};
반응형

'과거' 카테고리의 다른 글

[JS] Lexical Grammar, Language Element  (0) 2021.07.05
너 왜 코드 그렇게 짰어?  (0) 2021.07.05
Runtime Execution, State Control, Flow Control, Sync & Async  (0) 2021.06.17
JavaScript - 1 (Version Specifications)  (0) 2021.06.17
웹 앱, 자바스크립트  (0) 2021.06.16
'과거' 카테고리의 다른 글
  • [JS] Lexical Grammar, Language Element
  • 너 왜 코드 그렇게 짰어?
  • Runtime Execution, State Control, Flow Control, Sync & Async
  • JavaScript - 1 (Version Specifications)
optimy
optimy
See one, do one, teach one
  • optimy
    공자천주
    optimy
  • 전체
    오늘
    어제
    • All posts (27)
      • Note (4)
      • Book (0)
      • Do one (0)
      • Teach one (0)
      • 과거 (23)
  • 블로그 메뉴

    • 링크

      • github
    • 공지사항

    • 인기 글

    • 태그

      프론트엔드 개발환경
      탭 관리 프로그램
      State Control
      능동적 태도
      details marker 제거
      창 그룹화
      날개셋 한글 입력기
      version specifications
      Sync & Async
      Runtime Execution
      캡처후저장
      브라우저 렌더링
      이벤트 위임
      복사한것저장
      이미지파일저장
      JavaScript
      구현 패턴
      개발자 도구 활용
      콜맥
      쿼티
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    optimy
    Non-blocking JavaScript - 1
    상단으로

    티스토리툴바