1

I have noticed that there is no direct solution but also I can think of at least two ways of implementing it.

What I'd like to know what is the best solution performance-wise?

  • Turn a string into and array, use splice, return to string

  • Use string s = s.substring(0,i)+s.substring(i,s.lenght)

  • Something I've missed

EDIT: What's wrong with my question and is there some faster way to do this? I want to do this around a tousand times on a string so I need it to be fast

1
  • 1
    jsperf.com and substrings should be fast enough. Commented Jan 13, 2014 at 20:58

2 Answers 2

1

I runned it on a JSPERF and substrings is a little bit faster then doing a splice !

Edit:

substring is pretty fast, my computer ran this function:

var str = "I love cookies"
str = str.substring(0, 1) + " really" + str.substring(1);

756 333 times in 0.053 secondes !

Sign up to request clarification or add additional context in comments.

Comments

0

Here's a nice little Curry which you may want to benchmark.

I think its about as simple as it will get.

The second function's signature is identical to the Array.prototype.splice method.

function mutate(s) {
    return function splice() {
        var a = s.split('');
        Array.prototype.splice.apply(a, arguments);
        return a.join('');
    };
}

mutate('101')(1, 1, '1');

Hope this is what you're looking for!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.