Skip to content

Commit

Permalink
Add no-op class method, two arguments and throwing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
chendo committed Apr 10, 2016
1 parent c5e0653 commit f394520
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
46 changes: 44 additions & 2 deletions JSInteropBenchmarksTests/JSContextBenchmarksTests.swift
Expand Up @@ -14,15 +14,18 @@ import WebKit
var value : String { get set }

func getString(string: String) -> AnyObject
static func getString(string: String) -> AnyObject

static func doNothing()
static func getString(string: String) -> AnyObject
static func twoArgs(a: String, _ b: String) -> String
static func getObject() -> ElementJSExports

static func allTheData() -> [[String: String]]
static func throwException() -> Int
}

@objc class ElementJS : NSObject, ElementJSExports {
dynamic var value : String

static let data = [[String: String]](count: 1000, repeatedValue: ["foo": "bar"])

init (value: String) {
Expand All @@ -33,6 +36,14 @@ import WebKit
return string
}

class func doNothing() {

}

class func twoArgs(a: String, _ b: String) -> String {
return a + b
}

class func getString(name: String) -> AnyObject {
return name
}
Expand All @@ -45,6 +56,11 @@ import WebKit
class func allTheData() -> [[String : String]] {
return data
}

class func throwException() -> Int {
JSContext.currentContext().exception = JSValue(newErrorFromMessage: "bad", inContext: JSContext.currentContext())
return 0
}
}

class JSContextBenchmarksTests: XCTestCase {
Expand Down Expand Up @@ -100,6 +116,15 @@ class JSContextBenchmarksTests: XCTestCase {
}
}
}

func testStaticFuncExportCall() {
context.setObject(ElementJS.self, forKeyedSubscript: "ElementJS")
self.measureBlock { () -> Void in
self.go {
self.context.evaluateScript("ElementJS.doNothing()")
}
}
}

func testStaticFuncExportCallScalarReturn() {
context.setObject(ElementJS.self, forKeyedSubscript: "ElementJS")
Expand All @@ -110,6 +135,15 @@ class JSContextBenchmarksTests: XCTestCase {
}
}

func testStaticFuncExportCallTwoArgsScalarReturn() {
context.setObject(ElementJS.self, forKeyedSubscript: "ElementJS")
self.measureBlock { () -> Void in
self.go {
self.context.evaluateScript("ElementJS.twoArgs('foo', 'bar')")
}
}
}

func testStaticFuncExportCallObjReturn() {
context.setObject(ElementJS.self, forKeyedSubscript: "ElementJS")
self.measureBlock { () -> Void in
Expand Down Expand Up @@ -137,5 +171,13 @@ class JSContextBenchmarksTests: XCTestCase {
}
}

func testStaticFuncExportCallThrow() {
context.setObject(ElementJS.self, forKeyedSubscript: "ElementJS")
self.measureBlock { () -> Void in
self.go {
self.context.evaluateScript("try { ElementJS.throwException() } catch (ex) { ex }")
}
}
}
}

21 changes: 15 additions & 6 deletions README.md
Expand Up @@ -6,9 +6,18 @@ Performed on a 2013 MBP, 2.8G.

Each operation run 1000 times in measureBlock.

* JS to Swift String Return: 6 microseconds (4% stddev)
* JS to Swift Dictionary/Object Return: 17 microseconds (7% stddev)
* JS calling Swift class method and returning string: 23 microseconds (20% stddev)
* JS calling Swift class method and returning Swift Object: 26 microseconds
* JS calling Swift class method and chaining method call returning string: 34 microseconds
* JS Calling Swift class method and returning array of 1000 dictionaries: 552 microseconds
* JS to Swift
* String Return: 6 microseconds (4% stddev)
* Dictionary/Object Return: 17 microseconds (7% stddev)
* JS calling Swift
* closure: 15 microseconds (7% stddev)
* class method
* with no args: 13 microseconds (4% stddev)
* returning array of 1000 dictionaries: 514 microseconds (2% stddev)
* throwing exception: 23 microseconds (6% stddev)
* with 1 arg
* returning string: 16 microseconds (20% stddev)
* returning Swift Object: 20 microseconds (4% stddev)
* class method and chaining method call returning string: 23 microseconds (5% stddev)
* with 2 args and returning string: 19 microseconds (4% stddev)

0 comments on commit f394520

Please sign in to comment.