2

I'm building a chrome extension which will send strings of HTML to a server via POST requests.

I'd like to compress these strings before sending them as some of them could be quite large.

Are there any JavaScript libraries available which facilitate this?

David Tuite
  • 22,258
  • 25
  • 106
  • 176
  • @olliej has a very nice answer on this topic here: http://stackoverflow.com/questions/3640357/how-to-compress-a-string – Jovan Perovic Mar 13 '12 at 19:50
  • I don't have the answer but JS has very different performance characteristics from say C++ or C#. You probably need to do a litte benchmark to select the best algorithm. – usr Mar 14 '12 at 10:45

1 Answers1

0

I ended up using the RawDeflate library to achieve compression of around thirty to fifty percent. This performed better than all the methods listed in the SO question linked in the comments.

I wrote the following Ruby class for inflating data on the server.

require "zlib"
require "base64"

class Decompression
  # Decompress content sent to the server. 
  #
  # Usage:
  #   
  #   Decompression.decompress(params["raw_content"])
  #
  # Returns string.
  def self.decompress(string)
    decoded = Base64.decode64(string)
    inflate(decoded).force_encoding('UTF-8')
  end

private

  # https://stackoverflow.com/q/1361892/574190
  def self.inflate(string)
    zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
    buf = zstream.inflate(string)
    zstream.finish
    zstream.close
    buf
  end
end
Community
  • 1
  • 1
David Tuite
  • 22,258
  • 25
  • 106
  • 176