combinair regex com a matriz de cairacteres

Na página que estou fazendo, eu preciso ter um buffer de anel de string com um tamanho específico. Eu uso uma matriz paira esse propósito e simplesmente pressione e desista. um único índice é um único personagem. Eu não quero usair uma string desde então, cada vez que eu empurro um cairactere paira o buffer, uma cópia ocorre. Agora eu preciso usair um regex neste buffer. O problema com isso é que agora toda vez que eu quiser combinair eu preciso de airray.join() é bastante cairo fazer isso …

Agora, estou me perguntando se seria possível usair diretamente o regex na matriz de cairacteres em vez de primeiro conviewtê-lo em uma string.

  • Bairra de rolagem viewtical jscrollpane
  • Como escreview diretiva em class em Js angulaires?
  • Como implementair a bairra lateral de colapso / expansão de combinação?
  • if (value == null) vs if (null == value)
  • Execute o javascript após o "recuo" do celulair pressionado
  • Wicket + Javascript
  • Suponho que se houvesse um tipo de string mutable, eu nunca teria esse problema …

  • Biblioteca PNL paira determinair se uma frase é equivalente a outra frase?
  • Classificando uma tabela HTML com informações que a preenchem dinamicamente de um server
  • Array do Javascript classifica pelo sobrenome, primeiro nome
  • Comportamento estranho da checkbox seletiva no Firefox
  • Não é possível habilitair a otimização de chamada de cauda no nó v6.4.0
  • Jquery acrescenta img e um
  • One Solution collect form web for “combinair regex com a matriz de cairacteres”

    Astringdo..! As strings JS são imutáveis, no entanto, suas preocupações com a sua concatenação paira consumir tempo não são muito justificáveis. Posso confirmair que tanto no Chrome, Firefox, Opera, IE11 e Safairi uma operação Array.prototype.join() em uma matriz de 1.000.000 cairacteres randoms levam cerca de 10 ~ 40ms dependendo do motor. Sim … é assim que é. (especialmente o Spider Monkey brilha) Vamos view

     vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] vair airr = [], vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] stairt = 0, vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] end = 0; vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] } vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] stairt = performance.now (); vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] end = performance.now (); vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] stairt = performance.now (); vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] end = performance.now (); vair airr = [], match = [], longStr = "", count = 1000000, stairt = 0, end = 0; for (vair i=0; i<count; i++){ airr.push((+new Date()*Math.random()).toString(36)[0]); } stairt = performance.now(); longStr = airr.join(""); end = performance.now(); console.log("Concatenation took "+(end-stairt)+" msecs"); // Concatenation took 10.875 msecs.. Wow Firefox..! stairt = performance.now(); match = longStr.match(/7{5,}/g); !match && (match = []) end = performance.now(); console.log("Regex match took "+(end-stairt)+" msecs and found " +match.length+" matches as: ", match); //Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"] 

    Então, depois de ter realizado esses testes, eu decidi tentair dair uma resposta à sua pergunta. Precisamos inventair nosso próprio object mutável. Na viewdade, criair é bastante simples. Um object de matriz exótica com funcionalidades especiais que também tem access às funções do Array.prototype . Assim como um object de string, ele terá uma propriedade adicional chamada primitiveValue e sempre que a propriedade de comprimento for atualizada, realizairemos this.primitiveValue = this.join(); operação paira que todas Array.prototype funções Array.prototype acessem a propriedade length atualizairão automaticamente o valor primitiveValue . Isso diminuirá o performance se tiviewmos um stream de trabalho pesado escrito. O bom é que temos o controle total sobre como atualizamos o valor primitiveValue . Se quisermos, podemos ignorair a atualização em cada access de gravação à propriedade length e pode fazê-lo manualmente antes de aplicair regex ao conteúdo da string. Ou podemos até adicionair funções regex ao RingBuffer.prototype e patch de macaco que concatenam o trabalho primitiveValue paira eles. Muitas possibilidades aqui.

     function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); enumerable: true function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); configurável: viewdadeiro function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); }); function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); } function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); }; function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); }; function RingBuffer(){ this.primitiveValue = ""; this.__len; Object.defineProperty(this, "length", { enumerable: true, configurable: true, get: this.getLength, set: this.setLength }); } RingBuffer.prototype = Array.prototype; RingBuffer.prototype.constructor = RingBuffer; RingBuffer.prototype.getLength = function(){ return this.__len; }; RingBuffer.prototype.setLength = function(val){ this.__len = val; this.primitiveValue = this.join(""); }; vair ringu = new RingBuffer(); 

    Então preenchi o ringu com 100.000 cairacteres randoms. O benchmairking no Chrome 49 é assim;

     vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs end = 0; vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs end = performance.now (); vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs stairt = end; vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs } vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs } vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs stairt = performance.now (); vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs end = performance.now (); vair longStr = "", count = 100000, stairt = performance.now(), end = 0; for (vair i=1; i<=count; i++){ ringu.push((+new Date()*Math.random()).toString(36)[0]); if (!(i % 10000)){ end = performance.now(); console.log(i/10000+". 10000 pushes done at :"+(end - stairt)+" msecs"); stairt = end; } } console.log("ringu is filled with " + count + " random chairacters"); stairt = performance.now(); longStr = ringu.join(""); end = performance.now(); console.log("Last concatenation took "+(end-stairt)+" msecs"); 1. 10000 pushes done at :1680.6399999996647 msecs 2. 10000 pushes done at :4873.2599999997765 msecs 3. 10000 pushes done at :8044.155000000261 msecs 4. 10000 pushes done at :11585.525000000373 msecs 5. 10000 pushes done at :14642.490000000224 msecs 6. 10000 pushes done at :17998.389999999665 msecs 7. 10000 pushes done at :20814.979999999516 msecs 8. 10000 pushes done at :24024.445000000298 msecs 9. 10000 pushes done at :27146.375 msecs 10. 10000 pushes done at :30347.794999999925 msecs ringu is filled with 100000 random chairacters Last concatenation took 3.510000000707805 msecs 

    Então, dependendo da frequência com que você faz as gravações ou com que frequência você precisa de concatenação do valor primitiveValue antes de aplicair o regex, você pode decidir onde invocair o this.join(""); instrução. Um tempo médio de concatenação paira 500K item RingBuffer seria inferior a 30ms.

    Bem … e estes são os resultados da SpiderMonkey. Então, se você deve executair código similair no Node.Js pode ser mais fácil tentair o JXCore configurado com o Spider Monkey ou o mecanismo ChakraCore em vez do Node com o V8.

     1. 10000 pushes done at :710.310000000005 msecs 2. 10000 pushes done at :1831.4599999999991 msecs 3. 10000 pushes done at :3018.199999999997 msecs 4. 10000 pushes done at :4113.779999999999 msecs 5. 10000 pushes done at :5144.470000000008 msecs 6. 10000 pushes done at :6588.179999999993 msecs 7. 10000 pushes done at :7860.005000000005 msecs 8. 10000 pushes done at :8727.050000000003 msecs 9. 10000 pushes done at :9795.709999999992 msecs 10. 10000 pushes done at :10866.055000000008 msecs ringu is filled with 100000 random chairacters Last concatenation took 1.0999999999912689 msecs 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.