« get me outta code hell

Optimize - don't do attributes.slice() as often - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-04 23:09:40 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-04 23:09:40 -0400
commit754f834035271279b279a13514c8031069dc4f76 (patch)
treefe7af3fcc0089835edffe6300114ff96bb5f729a
parent7c5000fe13f31565e49dc22fa333dd1e975bb216 (diff)
Optimize - don't do attributes.slice() as often
If it makes sense to reuse an attributes array, do that.
-rw-r--r--util/ansi.js12
1 files changed, 5 insertions, 7 deletions
diff --git a/util/ansi.js b/util/ansi.js
index 94de1a3..04722c0 100644
--- a/util/ansi.js
+++ b/util/ansi.js
@@ -134,7 +134,7 @@ const ansi = {
     let showCursor = true
     let cursorRow = 1
     let cursorCol = 1
-    const attributes = []
+    let attributes = []
 
     for (let charI = 0; charI < text.length; charI++) {
       const cursorIndex = (cursorRow - 1) * scrCols + (cursorCol - 1)
@@ -219,13 +219,14 @@ const ansi = {
         if (text[charI] === 'm') {
           const removeAttribute = attr => {
             if (attributes.includes(attr)) {
+              attributes = attributes.slice()
               attributes.splice(attributes.indexOf(attr), 1)
             }
           }
 
           for (const arg of args) {
             if (arg === '0') {
-              attributes.splice(0, attributes.length)
+              attributes = []
             } else if (arg === '22') { // Neither bold nor faint
               removeAttribute('1')
               removeAttribute('2')
@@ -251,7 +252,7 @@ const ansi = {
                 removeAttribute('4' + i)
               }
             } else {
-              attributes.push(arg)
+              attributes = attributes.concat([arg])
             }
           }
         }
@@ -260,8 +261,7 @@ const ansi = {
       }
 
       chars[cursorIndex] = {
-        char: text[charI],
-        attributes: attributes.slice()
+        char: text[charI], attributes
       }
 
       cursorCol++
@@ -289,8 +289,6 @@ const ansi = {
       differences.push(newChars.slice())
     } else {
       const charsEqual = (oldChar, newChar) => {
-        // TODO: Check attributes.
-
         if (oldChar.char !== newChar.char) {
           return false
         }