performance of number to string

43 views
Skip to first unread message

Ben Echols

unread,
Nov 17, 2015, 6:53:39 PM11/17/15
to GopherJS
I have some go code that converts a bunch of numbers into strings. When gopherjs'd the conversion is extremely slow. I have tried both
fmt.Sprintf("foo %.2f bar", myNumber)
and
"foo " + strconv.FormatFloat(myNumber, 'f', 2, 64) + " bar"

then later in my code I iterate a list of numbers to generate a comma separate string:
"1,2,3,4"
for example like this
result := ""
for i:=0; i<len(numbers); i++ {
  result
+= strconv.FormatFloat(numbers[i], 'f', 2, 64) + ","
}


In my original javascript code I simply could just call myNumber.toString() and the performance was pretty good. 

What is the best way to improve the performance?

My thoughts have been: 
  1. Access the underlying number and call 'toString' on it (perhaps by overriding the strconv package?)
  2. In the case of the array of values I used to call numbers.join(","); perhaps I could find a way to do that?

I have some (probably pretty dirty) code that seems to make it quite fast:

 result := ""
 
for i := 0; i < len(numbers); i++ {
   val
:= float64(numbers[i]) / 100.0
   result
+= (js.InternalObject(val).Call("toString")).String() + ", "
 
}


And this brought up another idea - would it be possible to create a way of just putting a little JS inline for performance reasons? Maybe something like js.InlineCode(myjscode) that could do a simple transfer or setting of data for you.

Hopefully this isn't too many questions at once.

Ben


Reply all
Reply to author
Forward
0 new messages