# Loop unlooping in Javascript

DevFeed: [Loop unlooping in Javascript](<https://devfeed.tech/articles/loop-unlooping-in-javascript-27604.md>)

Original publisher: [Read original article](<https://gagor.pro/2014/01/loop-unlooping-in-javascript/>)

Author: Tom

Published: 2014-01-07T00:00:00Z

Content type: tutorial

Language: en

Sources: [Tomasz Gągor](<https://devfeed.tech/sources/tomasz-gagor.md>)

Topics: [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Scripting](<https://devfeed.tech/topics/scripting.md>)

Tags: [book](<https://devfeed.tech/tags/book.md>), [examples](<https://devfeed.tech/tags/examples.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [optimisation](<https://devfeed.tech/tags/optimisation.md>), [scripting](<https://devfeed.tech/tags/scripting.md>)

## AI overview

This short JavaScript article presents loop unrolling examples, including versions using a switch statement and a version without one. The author considers the second version more readable and notes that the examples could be translated into other scripting languages.

## Source excerpt

Few days ago I've read a book 'Even Faster Web Sites' about websites optimisation and I found one thing usefuluseful, not only on websites. There was a small tip about looploop unlooping. I want to quote them for later use. First - with switch statement var iterations = Math.ceil(values.length / 8); var startAt = values.length % 8; var i = 0; do { switch(startAt) { case 0: process(values[i++]); case 7: process(values[i++]); case 6: process(values[i++]); case 5: process(values[i++]); case 4: process(values[i++]); case 3: process(values[i++]); case 2: process(values[i++]); case 1: process(values[i++]); } startAt = 0; } while(--iterations > 0); Second - without switch var iterations = Math.floor(values.length / 8); var leftover = values.length % 8; var i = 0; if(leftover > 0) { do { process(values[i++]); } while(--leftover > 0); } do { process(values[i++]); process(values[i++]); process(values[i++]); process(values[i++]); process(values[i++]); process(values[i++]); process(values[i++]); process(values[i++]); } while (--iterations > 0); I found second example more readable and I prefer it. These examples after translation could be easily used in other scripting languages.