Skip to content

Commit

Permalink
(perf) Stop search immediately after finding acTL chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
vHeemstra authored Aug 10, 2024
2 parents 16510c7 + 4421386 commit 811fcfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ isApng(new Uint8Array(buffer))
#### As old-school global script tag

Url for latest version: `https://unpkg.com/is-apng`<br>
Url for specific version: `https://unpkg.com/is-apng@1.1.2/dist/index.js`
Url for specific version: `https://unpkg.com/is-apng@1.1.3/dist/index.js`

```html
<script src="https://unpkg.com/is-apng" type="text/javascript"></script>
Expand All @@ -53,7 +53,7 @@ Url for specific version: `https://unpkg.com/is-apng@1.1.2/dist/index.js`
#### As module

Url for latest version: `https://unpkg.com/is-apng/dist/index.mjs`<br>
Url for specific version: `https://unpkg.com/is-apng@1.1.2/dist/index.mjs`
Url for specific version: `https://unpkg.com/is-apng@1.1.3/dist/index.mjs`

```html
<script type="module">
Expand Down
28 changes: 12 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,28 @@ export default function isApng(buffer: Buffer | Uint8Array): boolean {

buffer = buffer.subarray(8)

let foundFirst = false
let firstIndex = 0
let secondIndex = 0
for (let i = 0; i < buffer.length; i++) {
if (
!foundFirst &&
(buffer[i] === sequences.animationControlChunk[firstIndex] ||
(firstIndex > 0 &&
((firstIndex = 0) ||
buffer[i] === sequences.animationControlChunk[firstIndex])))
) {
if (buffer[i] !== sequences.animationControlChunk[firstIndex]) {
firstIndex = 0
}

if (buffer[i] === sequences.animationControlChunk[firstIndex]) {
firstIndex++
if (firstIndex === sequences.animationControlChunk.length) {
foundFirst = true
return true
}
}

if (
buffer[i] === sequences.imageDataChunk[secondIndex] ||
(secondIndex > 0 &&
((secondIndex = 0) ||
buffer[i] === sequences.imageDataChunk[secondIndex]))
) {
if (buffer[i] !== sequences.imageDataChunk[secondIndex]) {
secondIndex = 0
}

if (buffer[i] === sequences.imageDataChunk[secondIndex]) {
secondIndex++
if (secondIndex === sequences.imageDataChunk.length) {
return foundFirst
return false
}
}
}
Expand Down

0 comments on commit 811fcfa

Please sign in to comment.