Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] slice method appears slower in loops than incrementally listing array indexes

Expand Messages
  • Marcel Duran
    What if you use regular expression with String.replace, you have a list of index plus you can alter the source data. Ex: var re = /
    Message 1 of 5 , Sep 4, 2010
    • 0 Attachment
      What if you use regular expression with String.replace, you have a list of
      index plus you can alter the source data. Ex:

      var re = /<style/gi,
      str = 'foobar<style foo>foobar</style>foobar<script
      bar>foobar</script><STYLE foo>foobar</STYLE>foobar',
      res = str.replace(re, function (match, index, source) {
      print(index); // 6 then 64
      return '*' + match + '*';
      });

      print(res); // foobar*<style* foo>foobar</style>foobar<script
      bar>foobar</script>*<STYLE* foo>foobar</STYLE>foobar


      Best,

      Marcel


      On Fri, Sep 3, 2010 at 7:55 PM, Cheney, Edward A SSG RES USAR <
      austin.cheney@...> wrote:

      >
      >
      > Marcel,
      >
      > I did consider those. The limitation of indexOf method is that it can only
      > be used once without altering your source data, and regular expression would
      > certainly be faster, but there is no native method for using a regular
      > expression to return a list of indexes.
      >
      > As far as which tests I was using I was altering the type_define function
      > of the markup_beauty function to obtain both tests. I used the it function
      > of the markupmin function to discover the exception situation. Both of those
      > scripts are components of a Pretty Diff tool I maintain at prettydiff.com
      >
      > http://prettydiff.com/markup_beauty.js
      > http://prettydiff.com/markupmin.js
      >
      > Thanks,
      > Austin
      >
      >



      --
      Marcel Duran


      [Non-text portions of this message have been removed]
    • sandyhead25
      Marcel, String replace would certainly be faster, because such instructions pass to the same code base on which the JavaScript interpreter sits. String
      Message 2 of 5 , Sep 5, 2010
      • 0 Attachment
        Marcel,

        String replace would certainly be faster, because such instructions pass to the same code base on which the JavaScript interpreter sits. String replace does not provide an index for its global matches so as to allow multiple instructions to execute on each match or pull each match into an empty array. In not for such conditions I would certainly use the replace method.

        It should be noted that while it is possible to pass a function with arguments into the replace method one should never do so. This essentially creates a whole through the interpreter to the underling code base. That means that if you can inject code into one of the function arguments you can pass that code through the JavaScript interpreter to possibly compromise the executing software. I would gladly sacrifice a measurable amount of performance as a general best practice to prevent potential code injection.

        Thanks,

        Austin
        http://prettydiff.com/
      Your message has been successfully submitted and would be delivered to recipients shortly.