Skip to content

String indexOf is extremely slow #2316

Closed

Description

@t2k269

The execution performance of indexOf method of String is extremely slow for long string (23000+ characters), the calls used around 10 seconds to complete. I tried to implement a rust method to override it. It used just 0.01s. please help to improve.

Thanks

Activity

  1. added
    A-Performance
    C-Builtins
    on Sep 29, 2022
  2. Razican commented on Sep 29, 2022

    @Razican
    Member

    Hello @t2k269, thank you for reporting this. Do you have an example string we can use to test this? Also, could you share the rust method you implemented to make this go faster?

  3. t2k269 commented on Sep 29, 2022

    @t2k269
    Author

    Hello Razican,

    The code is just try to find the match index of a string in a string with length around 20000.

    const a = "0123456789"
    let s = ""
    for (let i = 0; i < 1000; i++) {
      s += a;
    }
    s = s + "target" + s
    const start = new Date().getTime()
    let r = s.indexOf("target")
    log = `Result: ${r}, Used ${(new Date().getTime() - start) / 1000}s`
    

    I tried to run above code in BOA playground, but it just used 0.17s. Although it is just 0.17s, it is much slower than just running it in Google Chrome console.

    For my real project, I have updated BOA to use 0.16, then compile and run it in Linux. The execution time is around 10 seconds.

    The replacement rust method is just a simple closure to run rust's string::find just like

            context.register_global_closure("stringIndexOf", 1, |_, args, _context| -> Result<JsValue, JsValue> {
                let s = args[0].as_string().unwrap().to_string();
                let t = args[1].as_string().unwrap().to_string();
                Ok(match s.find(t.as_str()) {
                    Some(i) => JsValue::Integer(i.try_into().unwrap()),
                    None => JsValue::Integer(-1)
                })
            })?;
    

    Then, I swap the prototype in JS

    const oldStringIndexOf = String.prototype.indexOf;
    String.prototype.indexOf = function(s, i) {
        if (i !== undefined && i !== 0) {
            return oldStringIndexOf.call(this.toString(), s, i)
        }
        return stringIndexOf(this.toString(), s)
    }
    

    Thanks for your quick reply.

    Regards,
    Thomas

  4. Razican commented on Sep 30, 2022

    @Razican
    Member

    Thanks for the example!

    I have to say that the playground will always be slower than the chrome console, since the playground is a VM in Webassembly running JS.

    We will check if it's possible to optimize that function. Note that the function you propose is probably not 100% spec compliant, and sometimes, doing it spec compliant makes it slower. We will check it, though, to see if it could be optimized.

  5. t2k269 commented on Oct 1, 2022

    @t2k269
    Author

    Hello Razican,

    Thanks for your explanation. I understand that BOA must follow the JS spec. However, I notice that the indexOf slow problem appear only when I force to build my project with tag "v0.16". Previously, my project build with BOA dependency on "main" branch. On main branch, the indexOf is fast and I didn't notice any performance issue.

    The reason of why I suddenly build my project with tag "v0.16" is that I saw the news from BOA that supported "async/await" officially in v0.16. The hash in main branch that I used to build my project previously is 9dda8d3

    Hope these information can help to feature out the issue.

    Thanks!

  6. jedel1043 commented on Oct 1, 2022

    @jedel1043
    Member

    Thankfully, this is fixed by #1659. Running the original snippet on 11e5bde returns:

    "Result: 10000, Used 0.002s"
    
  7. added this to the v0.17.0 milestone on Oct 1, 2022
  8. t2k269 commented on Oct 2, 2022

    @t2k269
    Author

    I tried to run the slow script with following 3 BOA versions

    • rev = 9dda8d3
    • tag = v0.16
    • branch = main
    • rev = 0e6bb74 (latest v0.17)

    The execution time is 0.003s, 2.6s, 0.002s and 3.4s respectively.

    Therefore, the problem appear on v0.16 and the current v0.17. Thanks!

  9. Razican commented on Oct 2, 2022

    @Razican
    Member

    Hi @t2k269 yes, that PR has not been merged yet. You can test it in the utf16-str branch.

  10. t2k269 commented on Oct 2, 2022

    @t2k269
    Author

    Oh! Great. The execution time is 0.001s on utf16-str branch, extremely fast now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-PerformanceC-Builtins

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions