Coder Social home page Coder Social logo

Comments (1)

DyfanJones avatar DyfanJones commented on July 17, 2024

Found Rcpp solutions developed from: https://stackoverflow.com/questions/25609174/fast-escaping-deparsing-of-character-vectors-in-r

remotes::install_github("dyfanjones/paws/paws.common", ref="json_escape")
json_escape_unicode <- function(string) {
  from <- intToUtf8(1:31, multiple = TRUE)
  to <- paste0("\\u00", format(as.hexmode(1:31), width = 2))
  for (i in 1:31) {
    string <- gsub(from[i], to[i], string, fixed = TRUE)
  }
  return(string)
}

json_convert_string <- function(string) {
  replace <- list(
    c("\\", "\\\\"),
    c('"', '\\"'),
    c("\b", "\\b"),
    c("\f", "\\f"),
    c("\r", "\\r"),
    c("\t", "\\t"),
    c("\n", "\\n")
  )
  for (elem in replace) {
    string <- gsub(elem[1], elem[2], string, fixed = TRUE)
  }
  string <- json_escape_unicode(string)
  string <- sprintf('"%s"', string)
  return(string)
}

json_convert_string_stringi <- function(string) {
  from <- c("\\", '"', "\b", "\f", "\r", "\t", "\n")
  to <- c("\\\\", '\\"', "\\b", "\\f", "\\r", "\\t", "\\n")
  string <- stringi::stri_replace_all_fixed(string, from, to, vectorize_all =F)
  string <- json_escape_unicode_stringi(string)
  string <- sprintf('"%s"', string)
  return(string)
}

json_escape_unicode_stringi <- function(string) {
  from <- intToUtf8(1:31, multiple = T)
  to <- paste0("\\u00", format(as.hexmode(1:31), width = 2))
  string <- stringi::stri_replace_all_fixed(string, from, to, vectorize_all = F)
  return(string)
}

unicode <- c(intToUtf8(1:31, multiple = T), "\\", '"', "\b", "\f", "\r", "\t", "\n")
x <- c(1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)
bms <- lapply(x, \(i) {
  string <- paste0(sample(c(letters, LETTERS, unicode), i, replace = T), collapse = "")
  bm <- bench::mark(
    old_paws = json_convert_string(string),
    stringi = json_convert_string_stringi(string),
    new_paws = paws.common:::json_convert_string(string)
  )
  bm$id <- i
  return(bm)
})

Created on 2024-02-27 with reprex v2.1.0

image

From this we have significant performance improvement.

from paws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.